#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    43
    Rep Power
    14
    I had surgery this week and must not be able to think. Can someone explain the reason for the difference in the following:

    $result=mysql_db_query($database,"select items from table;");
    while ($myrow = mysql_fetch_array($result)) {
    $item=mysql_result($result,0,"items");
    echo "$item <br>";
    }
    (the one above just loops forever printing the same item over and over;

    vs (the same as above through the while statement then)
    $item=$myrow["items"];
    echo $school;

    This one does what I want which is print each item once.

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    41
    Rep Power
    15
    You're using two data retrieval methods in a single loop. That's incorrect. The correct syntax would be:

    $result=mysql_db_query($database,"select items from table;");
    while ($myrow = mysql_fetch_array($result)) {
    print "$myrow[item]<br>n";
    }

    or:
    $result=mysql_db_query($database,"select items from table;");
    $count = mysql_numrows($result);

    for ($i=0;$i<$count;$i++) {
    $item=mysql_result($result,$i,"items");
    print "$item<br>n";
    }
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4476
    You're performing two fetching operations on the same result. You fetch a row with fetch_array, then you get row zero with mysql_result. then the next time fetch_array executes, the result is back at row zero...so the loop continues endlessly.

    mysql_fetch_array and mysql_result both return data out of the results, you use one or the other, now both.

    $result=mysql_db_query($database,"select items from table;");
    while ($myrow = mysql_fetch_array($result)) {
    $item=myrow["items"];
    echo "$item <br>";
    }

    or

    $result=mysql_db_query($database,"select items from table;");
    $num_rows = mysql_num_rows($result);
    for($count=0;$count<$num_rows;$count++)
    {
    $item=mysql_result($result,$count,"items");
    echo "$item <br>";
    }

    Hope that helps...

    ---John Holmes...

    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions...
    *
    * PHP Manual. www.php.net/manual
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter
    *************************************************************
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4476
    Damn...I took to long typing and you beat me...

    Great minds think alike.

    ---JH

    [This message has been edited by SepodatiCreations (edited October 22, 2000).]

Similar Threads

  1. Replies: 2
    Last Post: August 18th, 2004, 10:45 PM
  2. Replies: 3
    Last Post: February 11th, 2004, 01:33 PM
  3. functions slowing code
    By dave2k in forum PHP Development
    Replies: 5
    Last Post: February 7th, 2004, 06:40 PM
  4. Ensuring a file's code doesn't cause error in another.
    By Jason Doucette in forum C Programming
    Replies: 0
    Last Post: February 6th, 2004, 12:51 PM
  5. vb code error combo box
    By xyfix in forum Visual Basic Programming
    Replies: 15
    Last Post: January 27th, 2004, 09:04 AM

IMN logo majestic logo threadwatch logo seochat tools logo