Closed Thread Icon

Topic awaiting preservation: record in first row not showing up in mysql_fetch_array() Pages that link to <a href="https://ozoneasylum.com/backlink?for=12602" title="Pages that link to Topic awaiting preservation: record in first row not showing up in mysql_fetch_array()" rel="nofollow" >Topic awaiting preservation: record in first row not showing up in mysql_fetch_array()\

 
Author Thread
norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 02-02-2003 20:57

I'm just full of php questions lately.....

In a page I have coded to display results from the table "results" the first entry in the "add" field is not showing up. Some how it is not finding it's way into the array. In the code below,I have tested this with ( commented out) echo $list[0], nothing shows up. This code echoes the rest of the entries, but skips over the first one.

<?php

function tell(){

mysql_connect("localhost","****") or

die("could not do the database thing for ya");

mysql_select_db("survey") or

die("could not select for ya");

$list=array();

$result=mysql_query("select * from results") ;

$row=mysql_fetch_array($result);

while ($row=mysql_fetch_array($result)){

if ($row["add"]!='0'){

$list[]=$row["add"];

}

}

//echo $list[0];


for($i=0;$i<count($list);$i++){

echo $list[$i];

echo "<br>";

}


mysql_free_result($result);

}

echo "<div align='center'><h1>Survey Statistics</h1>
<a href='admin.php'>view all<br>submissions</a></div>";
echo "<br><br><h3>Number of participants. ";

echo "</h3>";

echo tally();

echo "<br><br>";

?>

If anyone spots where I've gotten off track here, I would sure appreciate being steered in the right direction.

thanks!!!

/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 02-02-2003 21:14

You may be throwing it off in one of two places.

You shouldn't call mysql_fetch_array() twice in a row in this situation. Also, what value are you checking for when you check to see if $row['add'] != 0. Are you checking for a field that has no value, or the actual number 0?

Try this and see what it returns:

function tell(){

mysql_connect("localhost","****") or

die("could not do the database thing for ya");

mysql_select_db("survey") or

die("could not select for ya");

$list=array();

$result=mysql_query("select * from results") ;

while ($row=mysql_fetch_array($result)){

array_push($list, $row["add"]);

}

//echo $list[0];


for($i=0;$i<count($list);$i++){

echo $list[$i];

echo "<br>";

}


mysql_free_result($result);

}

echo "<div align='center'><h1>Survey Statistics</h1>
<a href='admin.php'>view all<br>submissions</a></div>";
echo "<br><br><h3>Number of participants. ";

echo "</h3>";

echo tally();

echo "<br><br>";

?>

If that returns what you want you can put the if() statement back in and then try it again.


-Butcher-

[This message has been edited by butcher (edited 02-02-2003).]

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 02-02-2003 21:19

yeah, what he said.

It's that first
$row = mysql_fetch_array(...
that 'eat's your first row. Simply as that, you never do anything with the $row variable, but reset it to the second row imediattly.

so long,

Tyberius Prime

norm
Paranoid (IV) Inmate

From: [s]underwater[/s] under-snow in Juneau
Insane since: Sep 2002

posted posted 02-02-2003 23:02

Thanks, Butcher & Tyberius Prime, for pointing me in the right direction.
I removed the offending first " $row ", and like magic, everything was spot on.

That's what I call " instant gratification " !!
( OK, maybe I should get out more often.)

In " if $row['add'] != 0{..... " I am checking for an actual value, a default value passed to MySQL if the user leaves a particular field blank.

I did not use the " array_push() " function because I am not familiar with it yet, I will study up on it today. I try not to use code I don't understand so that I can avoid the temptation to copy & paste.

Thanks a million !!!!


/* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 02-03-2003 05:27

That's the best way to do it! Study instead of blindly following a procedure. We need as much of that spirit as we can get around here!

The official PHP manual at PHP.net is the best language reference I've ever read (although my knowledge is admittedly limited). Whenever I want to do something with, for instance, strings, I tend to browse through the relevant functions, then read the submitted commments carefully to get a better idea of their abilities and drawbacks. Happy hunting!

« BackwardsOnwards »

Show Forum Drop Down Menu