SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with PHP While loop and MySQL

    Hello Guys

    This is giving me a real headache, I'm no PHP or MYSQL expert and I'm just baffled.

    I'm trying to query to one table to pull out data that I will then use to query a second table.

    In my test I expect rows 3 & 4 to be obtained.

    This works fine for the first loop, giving me row 3, but then instead of row 4 the loop outputs row 1, any ideas?

    I suspect that it is something to do with the While loop as when I use print_r() to output the array I'm looping it outputs the correct data.

    Here's my code (including code to test variable values):
    Code:
    					<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    
    $conn = mysql_connect($dbhost, $dbuser) or die                      ('Error connecting to mysql');
    
    $dbname = 'implit';
    mysql_select_db($dbname);
    $i = 0;
    $query  = "SELECT * FROM `jos_jreviews_comments` WHERE pid='15'";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    print_r($row);
    echo ("id = ".$row[$i][0].", ");
    $queryrating  = "SELECT * FROM `jos_jreviews_ratings` WHERE reviewid=".$row[$i][0]."";
    $resultrating = mysql_query($queryrating) or die(mysql_error());
    $rowrating = mysql_fetch_array($resultrating, MYSQL_NUM);
    if( ! empty($rowrating))
       printf("Rating: %s", $rowrating[1]);
    $i = $i+1;
    }
    
     ?>



    Any help is most appreciated.

    Thanks

    Paul

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mostly need some ORDER BY clause to have them ordered by your SQL select statements.

    Also, you could combine those 2 SELECT statements into a single one.

    And if you don't need all the data from your SELECT then enumerate the fields instead of doing SELECT * as this grabs all the data even if you don't use it (like in your first SELECT, you seem to only need the id, then simply do SELECT id FROM....)
    David


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •