?
Solved

error in script need help debugging

Posted on 2003-03-05
9
Medium Priority
?
211 Views
Last Modified: 2006-11-17
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
Comment
Question by:bartonjo2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8075206
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
 
LVL 3

Author Comment

by:bartonjo2
ID: 8075318
the changes didnt do anything at all even when i followed your test comment
0
 
LVL 3

Author Comment

by:bartonjo2
ID: 8075322
the changes didnt do anything at all even when i followed your test comment
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 15

Expert Comment

by:VGR
ID: 8075369
???
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
 
LVL 3

Author Comment

by:bartonjo2
ID: 8075709
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
 
LVL 15

Accepted Solution

by:
VGR earned 80 total points
ID: 8075927
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
 
LVL 15

Expert Comment

by:VGR
ID: 8077887
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
 
LVL 3

Author Comment

by:bartonjo2
ID: 8078927
VGR what does 'echo<<<EOS;' do?
0
 
LVL 15

Expert Comment

by:VGR
ID: 8079119
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question