日期:2014-05-17  浏览次数:20487 次

mysql_fetch_array()的那些事
调试程序时出现这个问题:mysql_fetch_array() expects parameter 1 to be resource, boolean given in
请问是什么问题?
程序如下:
$result2=mysql_query("select sum(score) as sum from ask_score where qid=".$qid."and answerid=".$authid);
$author3 = mysql_fetch_array ( $result2, MYSQL_ASSOC );
$scoresum=$author3["sum"];

$result3=mysql_query("select count(*) as count from ask_score where qid=".$qid);
$author4 = mysql_fetch_array ( $result3, MYSQL_ASSOC );
$scorenum=$author4["count"];
ask_score表:
CREATE TABLE ask_socre(
qid int(10) unsigned NOT NULL default '0',
authorid mediumint(8) unsigned NOT NULL default '0',
answerid mediumint(8) unsigned NOT NULL default '0',
vote_id mediumint(8) unsigned NOT NULL default '0',
score int(2) unsigned NOT NULL default '0',
PRIMARY KEY (qid,authorid)
);

------解决方案--------------------
你的mysql_query返回了一个false,sql语句错误
改成mysql_query(...) or die(mysql_error()); 看sql语句有什么错误
------解决方案--------------------
1.表名
2.加点空格,最好加上单引号
where qid=".$qid."and answerid=".$


where qid=".$qid." and answerid=".$


PHP code

<?php

$qid = '1';
$authid = '1';
$link = mysql_connect('127.0.0.1', 'root', '');

mysql_select_db("test");


$sql2= " select sum(score) as sum from ask_socre where qid='".$qid."' and answerid='".$authid."' ";
$result2=mysql_query($sql2);
echo $sql2;
if($author3 = mysql_fetch_array ( $result2, MYSQL_ASSOC )){ 
    var_dump($author3);
    $scoresum=$author3["sum"];
}

$sql3 = "select count(*) as count from ask_socre where qid='".$qid."' ";
$result3=mysql_query($sql3);
echo $sql3;
if($author4 = mysql_fetch_array ( $result3, MYSQL_ASSOC )){ 
    var_dump($author4);

    $scorenum=$author4["count"];
}