while循环中的mysql_fetch_array在localhost中工作但在主机服务器中不起作用

时间:2012-11-05 12:52:04

标签: php

$result = mysql_query('SELECT * FROM phpfox_education_question where subject_id = 2 and ques_id =1');
var_dump($result);
//var_dump result is :- "resource(64) of type (mysql result)" in localhost and web host,but
while($row = mysql_fetch_array($result))
{
    var_dump($row); //**NOTHING DISPLAYED in web host server but in localhost**
}

2 个答案:

答案 0 :(得分:0)

尝试一下:

$query = "SELECT * FROM phpfox_education_question WHERE subject_id = '2' AND ques_id = '1'";
$result = mysql_query($query) or die(mysql_error()); // <--- added this
echo "Results: ".mysql_num_rows($result)."<br />"; // <--- added this to output result count
while($row = mysql_fetch_array($result))
{
    print_r($row); //**NOTHING DISPLAYED in web host server but in localhost**
}

这是使用折旧代码btw

答案 1 :(得分:0)

尝试添加此内容:

var_dump(mysql_num_rows($result));

要查看是否有任何要提取的行,如果没有,那就是你的答案。
顺便说一句,在PDO中,这段代码如下:

$db = new PDO('mysql:dbname=default_db_name;host=127.0.0.1',$user,$pass);
//use prepared statmenets
$stmt = $db->prepare('SELECT * FROM your_db.phpfox_education_question WHERE subject_id = :subject_id AND ques_id = :ques_id');
//pass binds when executing the stmt
$stmt->execute(array(':subject_id' => 2, ':ques_id' => 1));
//number of rows found:
var_dump($stmt->rowCount());
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{//fetch rows as assoc array
    var_dump($row);
}

请注意,这是一个基本示例,您可以使用PDO进行更多操作

更新
要获得更多调试信息,请尝试以下操作:

try
{
    $db = new PDO();//connect, pass correct data here
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//cause PDO to throw PDOExceptions on error
    $stmt = $db->prepare('SELECT * FROM your_db.phpfox_education_question WHERE subject_id = :subject_id AND ques_id = :ques_id');
    $stmt->execute(array(':subject_id' => 1, ':ques_id' => 2));
    var_dump($stmt->rowCount());
    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        var_dump($row);
    }
}
catch(PDOException $e)
{
    echo $e->getMessage();
    $db = null;
    return;
}
echo 'No problems with query<br/>';
$db = null;//disconnecto

确保没有抛出任何错误,如果有,请检查消息,找出出错的地方并进行修复。如果你找不到解决问题的方法,我们当然可以提供帮助。