How can I fix this problem?Unable to jump to row 0

Discussion in 'PHP' started by Hannaspice, Sep 2, 2009.

  1. #1
    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:
     
    Hannaspice, Sep 2, 2009 IP
  2. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #2
    try echoing out $userid , $comment and $resul variables and you'll see what your problem is :)
     
    killerj, Sep 2, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    premiumscripts, Sep 2, 2009 IP
  4. shaunole

    shaunole Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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!
     
    shaunole, Sep 3, 2009 IP
  5. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    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.
     
    Hannaspice, Sep 4, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    premiumscripts, Sep 5, 2009 IP
  7. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #7
    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.
     
    Hannaspice, Sep 5, 2009 IP
  8. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Your entire code is wrong, already told you what to change, you haven't changed it. Have it your way then I guess.
     
    premiumscripts, Sep 6, 2009 IP
  9. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #9
    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:
     
    crazyryan, Sep 6, 2009 IP
  10. sarikabtech

    sarikabtech Well-Known Member

    Messages:
    186
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    108
    #10
    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
     
    sarikabtech, Sep 6, 2009 IP
  11. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #11
    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,
     
    Hannaspice, Sep 6, 2009 IP