Irksome Posted July 12, 2007 Share Posted July 12, 2007 Hi all. I've just started to make my own forum as a hobby, but have ran into this problem: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/public_html/forum/viewthread.php on line 45 This shows up whenever I try to view a thread. The code for the page is as follows: <?php require '../db.php'; $tbl_name="comforum_threads"; // Table name // get value of id that sent from address bar $id=$_GET['id']; $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="93%"><? echo $rows['datetime']; ?></td> <td width="7%"> </td> </tr> </table> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="8" valign="top"><? echo $rows['name']; ?></td> <td width="83%" valign="top"><p><? echo $rows['thread']; ?><hr></p> <p><? echo $rows['detail']; ?></p> </td> </tr> </table> <?php $tbl_name2="comforum_posts"; // Switch to table "comforum_posts" $sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){ ?> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="82%"><? echo $rows['a_datetime']; ?></td> <td width="18%"><? echo $rows['a_id']; ?></td> </tr> </table> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="8" valign="top"><? echo $rows['a_name']; ?> </td> <td width="83%" valign="top"><p><? echo $rows['thread']; ?><hr></p> <p><? echo $rows['a_answer']; ?></p></td> </tr> </table> <? } $sql3="SELECT view FROM $tbl_name WHERE id='$id'"; $result3=mysql_query($sql3); $rows=mysql_fetch_array($result3); $view=$rows['view']; // if have no counter value set counter = 1 if(empty($view)){ $view=1; $sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'"; $result4=mysql_query($sql4); } // count more value $addview=$view+1; $sql5="update $tbl_name set view='$addview' WHERE id='$id'"; $result5=mysql_query($sql5); mysql_close(); ?> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td><p>Quick Reply</p></td> </tr> </table> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="postprocess.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td width="11%"><strong>Name</strong></td> <td width="83%"><input name="a_name" type="text" id="a_name" size="45"></td> </tr> <tr> <td><strong>Email</strong></td> <td><input name="a_email" type="text" id="a_email" size="45"></td> </tr> <tr> <td valign="top"><strong>Message</strong></td> <td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" value="<? echo $id; ?>"></td> </tr> <tr> <td> </td> <td width="6%"><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> </tr> </table> </td> </form> </tr> </table></td> </tr> </table> <p> </p> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> </tr> </table> I also have another error when replying to a thread: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/public_html/forum/postprocess.php on line 15 ERROR The code for that page is: <?php require '../db.php'; $tbl_name="comforum_posts"; // Table name // 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='viewthread.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 "ERROR"; } mysql_close(); ?> I have no idea why it's doing it. Could anyone shed any light on this subject? Thanks. Quote Link to comment Share on other sites More sharing options...
piznac Posted July 12, 2007 Share Posted July 12, 2007 Im not sure where the error is coming from. But I would suggest everytime you run a "mysql_query" you also and "mysql_query($yourquery) or die ("this query error" . mysql_error())" or something like that to let you know if your queries are running fine. There could be a problem with your queries. It might not be that, but I feel its an very good practice. Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 You never check your query actually succeeds before trying to use its results. The general syntax for a SELECT statement should always be something like.... <?php // connect to db. if ($result = mysql_query($sql)) { // its now confirmed that $result holds a valid resource. if (mysql_num_rows($result)) { // its now confirmed that $result holds a resource which actually contains data. $row = mysql_fetch_assoc($result); } else { // no data found. } } else { // query failed. good place to debug eg; echo mysql_error() . "<br />$sql"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.