在php中获取查询结果的路径

时间:2011-08-26 16:43:20

标签: php sql ftp

我正试图获得以下$ result的直接路径:

$query="SELECT videoid from blabla";
$result=mysql_query($query);
$destination="ftp:/.../";

我正在尝试获取$ result中的videoid的直接路径,这样我就可以将所有实际的视频文件发送到ftp。

到目前为止,我有:

ftp_put ($connection,$destination,$result,FTP_ASCII);

当我运行它时,它说源必须是字符串或类似的东西。

3 个答案:

答案 0 :(得分:1)

$result不是您要查找的字段,您必须从$result获取数据。请注意mysql_fetch_assoc调用,它会从查询结果中提取数据。

以下是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);

您可以按照以下方式修改代码:

$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);

答案 1 :(得分:0)

$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 );

答案 2 :(得分:0)

你可能需要这样的东西:

选择一个视频:

// 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 */

选择所有视频:

// 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 */
}