PHP: mysql_fetch_array() expects parameter 1 to be resource, boolean given [duplicate]

瘦欲@ 提交于 2019-11-27 09:06:45

问题


Possible Duplicate:
MySQL & PHP Parameter 1 as Resource

I am getting shown in the title on my website and don't what kind of error this is, neither do I know how to fix this. Can anyone help me?

This is the add_answer.php file:

<?php
    include("mysql_forum_test.php"); // Get value of id that sent from hidden field
$id=$_POST['id'];

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form
$a_name=$_POST['a_name'];
$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer'];

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer
$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
$result2=mysql_query($sql2);

if($result2){
echo "Successful<BR>";
echo "<a href='index.php?content=view_topic?id=".$id."'>View your answer</a>";

// If added new answer, add value +1 in reply column
$tbl_name2="forum_question";
$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";
$result3=mysql_query($sql3);

}
else {
echo "ERROR";
}

mysql_close();
?>

Thanks


回答1:


Per the documentation, mysql_query returns FALSE on an error with the query. Because of this, your argument to mysql_fetch_array is a boolean. Use the mysql_error function to see what's wrong with the SELECT query.

For example,

$result=mysql_query($sql) or die(mysql_error());



回答2:


I suspect you don't actually have a database connection initiated. This means that mysql_query() fails and returns false, which is the bool described in the error.

Have a look at http://www.php.net/mysql_connect




回答3:


There must be an error in the query of some sort.

First off, I don't see a mysql_connect(). It is probably in an included file. Check to make sure there is no error for the connection (mysql_error() after the connection).

You can also check:

if (!mysql_query($sql)) {
   echo mysql_error();
}

That will show you any error in the query.

You should also look into SQL Injection and a DB Wrapper (I suggest PDO). PDO can be set up to throw an exception on errors so you will definitely know about them.



来源:https://stackoverflow.com/questions/7787097/php-mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!