-2

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

Community
  • 1
  • 1
Mitchell Verbeek
  • 19
  • 1
  • 1
  • 4
  • 2
    Please please _please_ go through the posts in the "related" section, on the right of this page. This type of question has been asked over and over. You're not checking the return values of the `mysql_*` functions. Until you do that, no one, including yourself, can help you. – Mat Oct 16 '11 at 20:33
  • Tip: Errors are quite common in the programming world. Learn how to handle errors. – hakre Oct 16 '11 at 20:34
  • 1
    Rolled back to actually question, replacing the question data with a short 'thanks' isn't helpful to the future. – Tim Lytle Oct 16 '11 at 20:53

3 Answers3

4

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());
imm
  • 5,837
  • 1
  • 26
  • 32
  • What is the mysql error function? – Mitchell Verbeek Oct 16 '11 at 20:35
  • 2
    @MitchellVerbeek: Learn to use the PHP manual. Every function is listed. It's written what it does, which parameters expected, the return values and even comments by users.: http://php.net/mysql_error – hakre Oct 16 '11 at 20:36
  • @imm Yes its included in mysql_forum_test.php. Ill try some of the suggestion atm. – Mitchell Verbeek Oct 16 '11 at 20:38
  • @MitchellVerbeek, see what error you get returned from `mysql_error`. If you still have a question about it, try opening a new question. – imm Oct 16 '11 at 20:39
  • Unknown column 'a_id' in 'field list' Hang on, I should be able to fix this myself :P – Mitchell Verbeek Oct 16 '11 at 20:41
  • Well, It tells me I do not have the field 'a_id' in my table, but I do have it, I checked. Any suggestions? – Mitchell Verbeek Oct 16 '11 at 20:45
  • @MitchellVerbeek, take a look at what SQL is actually being executed. i.e., echo the $sql variable. – imm Oct 16 '11 at 20:50
  • @imm can you point me at a syntax reference for what you're doing there? It looks like that should run the first part, evaluate it, then run the second part and evaluate it (which would always cause the program to die). – RonLugge Oct 16 '11 at 21:39
  • @RonLugge, see the manual page for logical operators, http://www.php.net/manual/en/language.operators.logical.php . The "Example #1" on that page illustrates the precedence difference between "||" and "or". – imm Oct 17 '11 at 00:37
  • @imm So what's happening is that the evaluation short-circuits and doesn't bother to evaluate the second half, since it's value will never matter after the first part returns true. Interesting, and completely not like what I would normally expect. Makes complete sense, even if it's not what my teachers taught me to think in terms of. Learning: it's what happens AFTER you finish your college education. (The precedence information is also interesting, but that was just a matter of placing parenthesis -- it didn't address my actual confusion, which was evaluating both sides before the 'or') – RonLugge Oct 17 '11 at 20:05
1

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

Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
1

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.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405