get path of a result of query in php
I am trying to get the direct path of the $result below:
$query="SELECT videoid from blabla";
$result=m开发者_如何学编程ysql_query($query);
$destination="ftp:/.../";
I am trying to get the direct path of the videoid which is in $result so i can send all the actual video files to an ftp.
So far I have:
ftp_put ($connection,$destination,$result,FTP_ASCII);
When I run it it says that the source has to be a string or something like that.
$result
is not the field your are looking for, you have to fetch the data from the $result
. Note the mysql_fetch_assoc
call which pulls data out of the query result.
Here is an example from the mysql_query documentation
// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string('fred'),
mysql_real_escape_string('fox'));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
You can therefore modify your code as follows:
$query="SELECT videoid from blabla";
$result=mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
// loop through every videoid returned, ftp each individually
ftp_put ($connection,$destination,$row['videoid'],FTP_ASCII);
}
mysql_free_result($result);
$query = "SELECT videoid from blabla";
$result = mysql_query( $query );
$row = mysql_fetch_row( $result );
$videoid = $row[0]; // <== there is your videoid
$destination="ftp:/.../";
ftp_put( $connection, $destination, $videoid, FTP_ASCII );
You probably need something like this:
To select one video:
// select all fields, not just the videoid
$query = "SELECT * FROM blabla WHERE videoid = 1"; // replace 1 with actual video id
// or only fields videoid and path
$query = "SELECT videoid, path FROM blabla WHERE videoid = 1"; // replace 1 with actual video id
$result = mysql_query( $query );
$row = mysql_fetch_assoc( $result );
$path = $row[ 'path' ]; // presuming 'path' is the column name
/* do some ftp stuff */
To select all videos:
// select all fields, not just the videoid
$query = "SELECT * FROM blabla";
// or only fields videoid and path
$query = "SELECT videoid, path FROM blabla";
$result = mysql_query( $query );
while( $row = mysql_fetch_assoc( $result ) )
{
$path = $row[ 'path' ]; // presuming 'path' is the column name
/* do some ftp stuff per row */
}
精彩评论