Hi ul, My problem is below, pls help me to fix this one: Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 12 in comment.php on line 38 $sql = "SELECT userid, comment FROM comments"; $result = mysql_query($sql); for($i=0; $i<mysql_num_rows($result); $i++) { $userid = ($result, $i, "userid"); $comment = ($result, $i, "comment"); $resul = mysql_query("SELECT id, username FROM users WHERE id='$userid'"); $user_comm = ($resul, 0, "username"); echo $user_comm; echo $comment; PHP:
You're not even fetching the rows.. Learn the basics. See http://www.php.net/mysql_fetch_assoc - it even has a usage sample right there. while ($row = mysql_fetch_assoc($result)) { echo $row["userid"]; echo $row["fullname"]; echo $row["userstatus"]; } PHP:
The "mysql_query($sql);" code actually returns an array object full of the records. It's not quite as simple as echoing out the array as it's an object. As premiumscripts mentioned, you need to loop through the array and handle the records within the loop. PHP provides a useful function called, mysql_fetch_accoc that you can loop through. That's where the code he posted comes from. You'll want to learn a little more about how MySQL works with PHP. Also, make sure you look into sanitizing and validating your data any time you use input from an end-user within a query. Your SQL query is currently vulnerable to sql injection unless you sanitize and validate the $userid variable outside of the code you have provided. Read more on sanitizing your data here. Good luck!
Thank you for replies, the problem is I am not good at programming, but I am trying to learn it more and more. I have 2 tables: 1. comments table 2. users table ------------------------------ 1. comments table: - comm_id - userid - guest - comment 2. users table: - user_id - username The problem is if there is a guest comments, userid will get 0 value and a notice alerts as below: Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 12 in comment.php on line 38 How can I fix so that the notice wont display?Or an idea, when a guest comments I submit $userid = $_POST['userid']; $userid = ""; What is your another suggestion, pls share it for me. $sql = "SELECT userid, comment FROM comments"; $result = mysql_query($sql); for($i=0; $i<mysql_num_rows($result); $i++) { $userid = ($result, $i, "userid"); $comment = ($result, $i, "comment"); $resul = mysql_query("SELECT id, username FROM users WHERE id='$userid'"); $user_comm = ($resul, 0, "username"); echo $user_comm\n; echo $comment\n; PHP: Many tks.
Already told you to do mysql_fetch_assoc and you're still not doing it.. Being a good programmer or a beginning programmer has nothing to do with this, you need to listen en research.
I think the problem is not 'cause of mysql_fetch_assoc, the problem is when guest comments, field `userid` gets 0 value, and an alert appears as above. How can I disappear that? Tks for quick reply.
Your entire code is wrong, already told you what to change, you haven't changed it. Have it your way then I guess.
If comment.user_id is set to 0 then obviously you can't tell who posted the comment. You need to post your addComment.php page. If that isn't what's happening and you just want some code to display comments, give this a whirl: <?php $query = mysql_query("SELECT * FROM `comment` LEFT JOIN `users` ON comment.user_id = users.user_id"); if(mysql_num_rows($query) == 0) { echo "There are no comments in the database."; } else { while($row = mysql_fetch_array($query)) { echo "<pre>"; print_r($row); echo "</pre>"; } } ?> PHP:
1. As "premiumscripts" said... use while and fetch the result into a variable (as below). while ($row = mysql_fetch_assoc($result)) 2. The warning "Unable to jump to row 0 on MySQL result index 12 ....." shows that result returned by MySQL is too less. So, use proper conditional statements (if...else...) to check if what you fetching is really there or not. Thanks, Sarika
many thanks to you guys, 'specially to premiumscripts I managed to do it, just as below if(!$userid == "0"){$out .= ".$user_comm."}else{$out .= ".$guest."} cheers,