Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL Loop - Multiple Rows

Options
  • 17-02-2012 10:08am
    #1
    Registered Users Posts: 8,004 ✭✭✭


    Hi Folks,

    Currently migrating from the old SQL to the new SQL methods in PHP (I learnt from a very old book)

    Here is my script:

    //Set the Query
    $query = "SELECT * FROM accounts WHERE name LIKE '%".$searchString."%'";
    echo $query; //For debug
    
    //Run the Query
    $result = mysql_query($query, $link);
    if (!$result) {
        echo "DB Error, could not query the database\n";
        exit;
    }
    
    $num_rows = mysql_num_rows($result); //Get the number of results
    echo $num_rows; //Works fine. Get 2
    
    for ($i=0; $i < $num_rows; $i++) {
    $arry = mysql_fetch_assoc($row);
    echo $arry['fieldName'];
    
    }
    
    

    The problem is the last bit. It only echoes one row and finishes. I know there are two results as my $num_rows echoes 2. The older script worked fine.

    I would be looking for the eqvilent of this:
    $result = $db->query($query);
    $num_results = $result->num_rows;
    $row = $result->fetch_assoc();
    
    for ($i=0; $i < $num_results; $i++) {
    $row = $result->fetch_assoc();
    echo $row['name'];
    }
    

    Any help greatly appreciated. Its probably just one line somewhere.

    Thank you!


Comments

  • Registered Users Posts: 1,990 ✭✭✭lynchie


    Look at the code you included. Where is $row declared.. It should be the resultset you are passing to it?


  • Registered Users Posts: 105 ✭✭damoth


    what about ...
    while ($row= mysql_fetch_assoc($result)) {
         echo $row['fieldName'];
    }
    

    or
    while ($row = $result->fetch_assoc()) {
         echo $row['name'];
    }
    


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    As lynchie says, you have $row where $result should be.

    Tbh, I find mysql_num_rows() and mysql_affected_rows() to be a little unreliable. I don't know why, I just don't really trust them :D

    The way that I would always get rows from a resultset is as below
    $result = mysql_query($query, $link);
    
    while($row = mysql_fetch_assoc($result)) {
     // Do Stuff
    }
    

    When you put an assignment statement into the condition of a while loop, it first carries out the assignment, then checks the value of the assigned variable.

    In this case, it does the operation $row = mysql_fetch_assoc and then checks the value of $row.
    If mysql_fetch_assoc returns data, then $row will be TRUE (any value != 0 is TRUE in PHP) and the contents of the while statement are executed. If mysql_fetch_assoc does not return data (because you are at the end of the resultset or there is no data), then it returns 0 and sets $row = 0. This then results in a FALSE in the condition and the while loop exits.

    The only benefit of doing it this way is slightly neater and more efficient code.


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    Got it working, in a nutshell:
    
    $result = mysql_query($query, $link);//Run the query
    
    while($row = mysql_fetch_array($result)) {
    echo $row['name']; //Echos every row in turn
    }
    
    

    Thank you for the reply.


Advertisement