Ok, So I am working on a script, I get this error - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/qaklock/public_html/add_answer.php on line 10 Code (markup): Here is line 7-10 : // 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); Code (markup): I think it is giving the error due to the query being empty, but how can I fix it from erroring and letting the script do what it is suppose to do. Here is the full part of this piece of script - <?php include("connect.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='view_topic.php?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 $query; $results = MySQL_Query($query) or die(MySQL_Error()); } mysql_close(); ?> Code (markup):
In order to better help you out can u post here sample DB results in table which match this query Does this query allways gives error or sometimes to remove error change this line $rows=mysql_fetch_array($result); to $rows=@mysql_fetch_array($result); Regards Alex
It says - Query was empty and @kmap - I just tried that and it just doesn't give the error, but doesn't do what it is suppose to do, which is to post the data input into the text boxes to the database and then display a link back to the topic.
if (isset($_POST['id'])) { $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; } } else { $max_id=1; }
i have asked you to post sample db results that atch your query If your db has no results matching query then it will be empty everytime Make sure you have the data in table Please post some data here Regards Alex
Well the code is still basically the same - <?php include("connect.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='view_topic.php?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 $query; $results = MySQL_Query($query) or die(MySQL_Error()); } mysql_close(); ?> Code (markup): and also, There is no data in the database for this part of the script, But I want it to work so that I dont have to put nulled data or something in there. If you guys want, Please PM for a link to the script on my server.
ok so lets change to this: if ($rows=mysql_fetch_array($result)) { $max_id = $rows['Maxa_id']+1; } else { $max_id = 1; }
you tried: if ($rows=mysql_fetch_assoc($result)) { $max_id = $rows['Maxa_id']+1; } else { $max_id = 1; } and that didnt work? there is always error_reporting(0);
that code should have replaced: $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; }
lol if you donot have any data how you expecting it to work It is allways going to show empty results Regards Alex
Try this, I am sure you required this: #First prevent SQL Injection #if $_POST['id'] is integer use [B]intval[/B]($_POST['id']), and if it is string use [B]htmlspecialchars[/B]($_POST['id']) #------------------------------------------------------------------------------------------------- $id=$_POST['id']; #Talking about this for SQL Injection // Find highest answer number. $sql="SELECT MAX(a_id) AS `maxa_id` FROM `$tbl_name` WHERE `question_id`='$id'"; $result=[B]mysql_query[/B]($sql); if([B]$result === FALSE[/B]) { echo "1:".mysql_error(); #query has error @mysql_close(); die(); } // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1 if([B]mysql_num_rows[/B]($result) ==0) { $max_id = 1; } else { $rows = [B]mysql_fetch_assoc[/B]($result); $max_id = $rows['maxa_id'] + 1; } Code (markup):