• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

error in script need help debugging

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>



0
bartonjo2
Asked:
bartonjo2
  • 5
  • 4
1 Solution
 
VGRCommented:
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<BR>"; 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<BR>";

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>
0
 
bartonjo2Author Commented:
the changes didnt do anything at all even when i followed your test comment
0
 
bartonjo2Author Commented:
the changes didnt do anything at all even when i followed your test comment
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VGRCommented:
???
how many displayed echo "num_rows is $num_rows<BR>";

 ?

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...
0
 
bartonjo2Author Commented:
i did uncomment it,
five rows should appear each full of information
none displayed how many displayed echo "num_rows is $num_rows<BR>";
0
 
VGRCommented:
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<BR>";

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>

0
 
VGRCommented:
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...
0
 
bartonjo2Author Commented:
VGR what does 'echo<<<EOS;' do?
0
 
VGRCommented:
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<BR>
[...]
<FORM ACTION="$somevariable">
[...]
EOS; // left-aligned

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now