-1

您好,我在使此代码正常工作时遇到了一些麻烦。

<?php
$filename = "test.php";
$filehandle = fopen($filename, 'a') or die("can't open file");
$test= 
    '
        $conn = mysql_connect("server", "username", "password" ) or die (mysql_error()) ;
        mysql_select_db("db", $conn);
        $query = "select * from test";
        $result =  mysql_query ($query);
        $row = mysql_fetch_array( $result );

        echo $row[\'test\'];

    ';
fwrite($filehandle, $test);
fclose($filehandle);
?>

而不是输出查询的结果,而是输出查询,我想这是因为我将完整的查询存储在一个变量中,有谁知道我将如何输出查询的结果而不是实际的查询? 谢谢大家的帮助。

4

3 回答 3

1

你不认为它应该是这样的:

<?php
$conn = mysql_connect("server", "username", "password" ) or die (mysql_error()) ;
mysql_select_db("db", $conn);
$query = "select * from test";
$result =  mysql_query ($query);
$row = mysql_fetch_array( $result );
$test = $row['test'];
$filename = "test.php";
$filehandle = fopen($filename, 'a') or die("can't open file");
fwrite($filehandle, $test);
fclose($filehandle);
?>
于 2013-10-12T19:54:28.927 回答
1
<?php
$filename = "test.php";
$filehandle = fopen($filename, 'a') or die("can't open file");

// do the query
$conn = mysql_connect("server", "username", "password" ) or die (mysql_error()) ;
mysql_select_db("db", $conn);
$query = "select * from test";
$result =  mysql_query ($query);

// get the result row
$row = mysql_fetch_array( $result );

// write a field to file
fwrite($filehandle, $row['test']);
fclose($filehandle);
?>
于 2013-10-12T19:57:33.493 回答
1

您应该使用 MySQLi 或 PDO

但是,您的代码应如下所示:

$filename = "test.php";
$filehandle = fopen( $filename ,'a' ) or die( "can't open file" );
$conn = mysql_connect( "server" ,"username" ,"password" ) or die ( mysql_error() ) ;
mysql_select_db( "db" ,$conn );
$query = "select * from test";
$result =  mysql_query( $query );
//  Iterate through results
while( $row = mysql_fetch_array( $result ) )
{
//  var_dump[$row];
    //  Replace your-column-name below with the actual name :
    fwrite( $filehandle ,$row['your-column-name'] );
}
fclose( $filehandle );

从手册页中查找一些示例:mysql_fetch_arraymysql_fetch_assocmysql_fetch_array

于 2013-10-12T19:59:58.677 回答