PHP,MYSQL while循环只显示最后一条记录

时间:2013-11-10 19:44:00

标签: php mysql

我的代码是 -

$dbhost = 'localhost';    
$dbuser = 'root';    
$dbpass = '';  
$dbname = 'moviefone';    
$con = mysql_connect($dbhost, $dbuser, $dbpass);  
mysql_select_db($dbname, $con);    

// Check connection    
if (mysqli_connect_errno()) {  
    echo "Failed to connect to MySQL: " . mysqli_connect_error();    
}    

$data = mysql_query("SELECT * FROM new_hindi") or die(mysql_error());
$info=NULL;
while ($row = mysql_fetch_array($data)){
    $info=$row;
}

?>

我的问题是循环只显示“new_hindi”表中的最后一条记录。

1 个答案:

答案 0 :(得分:0)

每次迭代时,都会覆盖$info变量。您必须向$info数组添加行。

$dbhost = 'localhost';    
$dbuser = 'root';    
$dbpass = '';  
$dbname = 'moviefone';    
$con = mysql_connect($dbhost, $dbuser, $dbpass);  
mysql_select_db($dbname, $con);    

// Check connection    
if (mysqli_connect_errno()) {  
    echo "Failed to connect to MySQL: " . mysqli_connect_error();    
}    

$data = mysql_query("SELECT * FROM new_hindi") or die(mysql_error());
$info=array();
while ($row = mysql_fetch_array($data)){
    $info[]=$row;
}

?>