visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: error in script need help debugging


By: PHP newbee U.S.A.  Date: 05/03/2003 00:00:00  English  Points: 20 Status: Answered
Quality : Excellent
i am writing a script to display the information ina mysql table in a table the generates ok but the information only appears in two rows instead of all of them, heres my code:


<table width="355" border="0" cellspacing="3" cellpadding="0">
<tr bgcolor="#B9CDD9">
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Id</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Name</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Rank</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Position</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Required</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Nrequired</font></th>
</tr>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("scouts", $db);

$result = mysql_query("SELECT * FROM scouts", $db);
$num_rows = mysql_num_rows($result);

for ($i = 0; $i < $num_rows; $i++) {
$myrow = mysql_fetch_row($result, $i);
$field1 = $myrow[0];
$field2 = $myrow[1];
$field3 = $myrow[2];
$field4 = $myrow[3];
$field5 = $myrow[4];
$field6 = $myrow[5];

?>
<tr>
<td height=19 bgcolor="#CCCCCC"><?php echo "$field1"; ?></td>
<td bgcolor="#DDDDDD"><?php echo "$field2"; ?></td>
<td bgcolor="#CCCCCC"><?php echo "$field3"; ?></td>
<td bgcolor="#DDDDDD"><?php echo "$field4"; ?></td>
<td bgcolor="#CCCCCC"><?php echo "$field5"; ?></td>

<td bgcolor="#DDDDDD"><?php echo "$field6"; ?></td>
</tr>
<?
}
?>

</table>




By: VGR Date: 05/03/2003 08:52:00 English  Type : Comment
ok, not that bad 8-)

I don't know why you've only 2 rows showing, but I would personally FIRST check that you've more than 2 rows coming from the SELECT :D :D

So just do an echo "$num_rows
"; before the for loop, just in case :D

I also see that the database "scouts" contains a table "scouts" : bad idea 8-)

Otherwise, I would recommend to rewrite your code this way :

<table width="355" border="0" cellspacing="3" cellpadding="0">
<tr bgcolor="#B9CDD9">
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Id</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Name</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Rank</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Position</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Required</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Nrequired</font></th>
</tr>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("scouts", $db);

$result = mysql_query("SELECT * FROM scouts", $db);
$num_rows = mysql_num_rows($result);

// here to test : echo "num_rows is $num_rows
";

for ($i = 0; $i < $num_rows; $i++) {
$myrow = mysql_fetch_row($result, $i);
$field1 = $myrow[0];
$field2 = $myrow[1];
$field3 = $myrow[2];
$field4 = $myrow[3];
$field5 = $myrow[4];
$field6 = $myrow[5];
echo <<<EOS
<tr>
<td height=19 bgcolor="#CCCCCC">$field1</td>
<td bgcolor="#DDDDDD">$field2</td>
<td bgcolor="#CCCCCC">$field3</td>
<td bgcolor="#DDDDDD">$field4</td>
<td bgcolor="#CCCCCC">$field5</td>
<td bgcolor="#DDDDDD">$field6</td>
</tr>
EOS;
} // for
?>


I would also have written a while loop on $res=$mysql_fetch_array($result) in stead of for() but anyway, it should also work...
</table>
By: PHP newbee Date: 05/03/2003 09:05:00 English  Type : Comment
the changes didnt do anything at all even when i followed your test comment
By: VGR Date: 05/03/2003 09:11:00 English  Type : Comment
???
how many displayed echo "num_rows is $num_rows
";

?

how many rows are coming from the SELECT statement ?
2 ?
More ?
The loop (whatever version) does still display 2 rows ?

I hope you've uncommented the test comment, at least...
By: PHP newbee Date: 05/03/2003 09:49:00 English  Type : Comment
i did uncomment it,
five rows should appear each full of information
none displayed how many displayed echo "num_rows is $num_rows
";
By: VGR Date: 05/03/2003 10:22:00 English  Type : Answer
ok
first from my PHP documentation I don't understand this line of yours :
$myrow = mysql_fetch_row($result, $i);

Signature is :
(PHP 3, PHP 4 >= 4.0.0)
array mysql_fetch_row (resource result)



Let's do it my way :D we'll see if it works 8-)

<table width="355" border="0" cellspacing="3" cellpadding="0">
<tr bgcolor="#B9CDD9">
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Id</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Name</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Rank</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Position</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Required</font></th>
<th><font size="2" face="Geneva, Arial, Helvetica,
sans-serif">Nrequired</font></th>
</tr>
<?php
$db = mysql_connect("localhost", "root") or die ("Could not connect");

mysql_select_db("scouts", $db) or die ("bad select DB ".mysql_error());

$query="SELECT * FROM scouts;";
$result = mysql_query($query, $db) or die ("SQL error in query '$query' : ".mysql_error());
$num_rows = mysql_numrows($result);

echo "num_rows is $num_rows
";

while ($myrow=mysql_fetch_array($result)) {
$field1 = $myrow[0];
$field2 = $myrow[1];
$field3 = $myrow[2];
$field4 = $myrow[3];
$field5 = $myrow[4];
$field6 = $myrow[5];
echo <<<EOS
<tr>
<td height=19 bgcolor="#CCCCCC">$field1</td>
<td bgcolor="#DDDDDD">$field2</td>
<td bgcolor="#CCCCCC">$field3</td>
<td bgcolor="#DDDDDD">$field4</td>
<td bgcolor="#CCCCCC">$field5</td>
<td bgcolor="#DDDDDD">$field6</td>
</tr>
EOS;
} // while
?>

</table>


By: VGR Date: 05/03/2003 17:31:00 English  Type : Comment
I'm not surprised that my code works (it's simple as B+A=BA) but I'm surprised it's behaving differently from yours. They are conceptually the same...
By: PHP newbee Date: 06/03/2003 21:57:00 English  Type : Comment
VGR what does 'echo<<<EOS;' do?
By: VGR Date: 06/03/2003 22:47:00 English  Type : Comment
it's one of the formidable features of PHP ;-)
basically it's an echo command (like print), so the following string is echoed to the HTML page générated, but in this case it telles the interpreter to consider everything following as the string (even 10 pages of HTML, line breaks, everything but PHP closing tag ?> of course) until the character specified after <<< is encountered alone on its line (no spaces before)

The benefit is you don't have to escape double quotes inside a string like in :
echo "this is a string with \" quotes"; // (especially painful when echoing A HREF or FORM tags)

Also : you don't have to do this to echo PHP variables inside the HTML code :
<FORM ACTION="<? echo $somevariable; ?>">;

you just write

echo <<<EOS
this is a string with " quotes

[...]
<FORM ACTION="$somevariable">
[...]
EOS; // left-aligned

It's fun, reliable, clearer, easier... bref, c'est bel et bon.

Do register to be able to answer

EContact
browser fav
page generated in 307.951930 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page