I'm trying to pull a set of numbers out of a database. The SQL statement works great in MySQL, I get all the numbers in the field returned, but I can't get it to work in PHP. All it gets out of the db is the highest number in that field.

<?
$conn = mysql_connect("localhost", "uname", "pwerd") or die ("Could not connect to db");
mysql_select_db('heardya_com') or die("Could not select the database!");
$queryMinPic = "SELECT picNum FROM pics";
$resPicNum = mysql_query($queryMinPic, $conn);
if (!$resPicNum) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $queryMinPic;
   die($message);
}
$picNum = mysql_fetch_array($resPicNum);
print_r($picNum);
?>

    Try this

    while ($row = mysql_fetch_array($picNum)){
       foreach ($row as $key => $value){
       print $key.' '.$value;
    }
    }

    instead of

    print_r($picNum);

    to iterate through the result set.

      while ($row = mysql_fetch_array($picNum)){
      foreach ($row as $key => $value){
      print $key.' '.$value;
      }
      }

      I can't believe I didn't think of this! The only problem that I can't figure out is why it prints the numbers twice.....

        got it
        while ($row = mysql_fetch_array($picNum))
        should be
        while ($row = mysql_fetch_assoc($picNum))

          Or better would be

          while ($row = mysql_fetch_row($picNum))

          and then you only need to print the $value part of the loop.

            Write a Reply...