Re: [PHP] var_dump() results



At 12:32 PM -0500 1/13/08, Europus wrote:
It's pretty much the same. With var_dump(), values from the first row
of the table are iterated twice, the script is not looping through and
reporting all 2100 rows. Grossly similar behavior was observed with
print_r() and echo: while different data was reported by each, the
loop wouldn't loop. The first row was reported once (or twice) and
that was the end of that. I've tried foreach() also, same-same. It
doesn't loop, it just reports the first row.

Here's my code:

<?php

//connect to db
$link = mysql_pconnect('$host', '$login', '$passwd');
if (!$link) {
die('Unable to connect : ' . mysql_error());
}
// make $table the current db
$db_selected = mysql_select_db('$table', $link);
if (!$db_selected) {
die ('Unable to select $table : ' . mysql_error());
}

//get column data
$sql = "SELECT column2, column3 FROM $table";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);

//loop through to display results
for($i=0; $i < count($row); $i++){
var_dump($row);
echo "<br /><br />";
?>

Ulex


As far as displaying only the first row, that's because you're only fetching the first row; count($row) in your for loop will always evaluate to 0 (if there are no results) or 1. You need to move the mysql_fetch_row() call into your loop; something like

$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
var_dump($row);
...
}

or you could do

$result = mysql_query($sql);
$count = mysql_num_rows($result);
for ($i=0; $i<$count; $i++) {
var_dump($row);
...
}

As far as displaying the results twice, I'm not sure; that resembles the behavior of mysql_fetch_array() -

http://us.php.net/manual/en/function.mysql-fetch-array.php

- where the default is to return data as both an associative and numerically-indexed array. I'm guessing that the above code isn't an exact cut-and-paste, as using single quotes (eg, mysql_pconnect('$host', '$login', '$passwd') does not interpolate variables, so that line will not work unless your username is actually '$login', etc. Which it probably isn't. Are you sure the real code isn't using mysql_fetch_array() instead of mysql_fetch_row()?

steve


--
+--------------- my people are the people of the dessert, ---------------+
| Steve Edberg http://pgfsun.ucdavis.edu/ |
| UC Davis Genome Center sbedberg@xxxxxxxxxxx |
| Bioinformatics programming/database/sysadmin (530)754-9127 |
+---------------- said t e lawrence, picking up his fork ----------------+
.



Relevant Pages

  • var_dump() results
    ... values from the first row ... of the table are iterated twice, the script is not looping through and ... reporting all 2100 rows. ... loop wouldn't loop. ...
    (php.general)
  • Re: [PHP] var_dump() results
    ... of the table are iterated twice, the script is not looping through and ... reporting all 2100 rows. ... loop wouldn't loop. ... it just reports the first row. ...
    (php.general)
  • Summary: Sol8 and EVA hangs
    ... Oct 8 11:00:41 proddb SCSI transport failed: reason 'aborted': ... retrying command ... # Adapter hard loop ID address to use on the fibre channel bus. ... # before reporting I/O errors. ...
    (SunManagers)
  • Re: The Dead Albatross Sketch
    ... BBC News 24 were reporting around twice an hour from a ... Sainsburys in West London. ...
    (uk.local.surrey)
  • Re: [PHP] var_dump() results
    ... of the table are iterated twice, the script is not looping through and ... reporting all 2100 rows. ... loop wouldn't loop. ... Ditto for ALL your values throughout this script. ...
    (php.general)