Jump to content

[SOLVED] elusive bug in code


bluethundr

Recommended Posts

Hi guys,

 

I am writing a simple php script to access a MySQL database to retrieve info from some tables and format them with html. Simple right? Well for some reason there is a bug in the code where all it does is print '<r>' over and over again in an infinite loop. I have stared at this code till my eyes bled. Can I get some assistance?

<!DOCTYPE HTML PUBLIC 
                    "-//W3C/DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
     <meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1">
     <title>Wines</title>
</head>
<body>
<?php
    require 'db.inc';
    
    
    
    // Show the wines in the HTML <table>
    function displayWines($result)
    {
    
    
       print "<h1>Our Wines</h1>\n";
       
       // Start a table, with column header
       print "\n<table>\n<tr>\n"  .
             "\n\t<th>Wine ID</th>" .
             "\n\t<th>Wine Name</th>" .
             "\n\t<th>Type</th>" .
             "\n\t<th>Year</th>" .
             "\n\t<th>Winery ID</th>" .
             "\n\t<th>Description</th>" .
             "\n</tr>";
             
             
       // Until there are no rows in the result set, fetch a row into
       // the $row array and ...
       while ($row = @ mysql_fetch_row($result))
       { 
          
          while ($row = @ mysql_fetch_row($result))
          {
             // ... start a TABLE row ...
             print "\n<tr>";
             
             // ... and print out each of the attributes in that row as a 
             // separate TD (Table Data).
             foreach($row as $data)
                   print "\n\t<td> {$data} </td>";
                   
             // Finish the row
             print "\n<\tr>";
          }
         
         // Then, finish the table
         print "\n</table>\n";
     }
     
     
   } 
   
   $query = "SELECT * FROM wine";
   
   // Connect to the MySQL server
   if (!($connection = @ mysql_connect($hostname, $username, $password)))
        die("Cannot connect");
        
   if (!(mysql_select_db($databaseName, $connection)))
       showerror();
       
   // Run the query on the connection
   if (!($result = @ mysql_query ($query, $connection)))
       showerror();
       
   // Display the results
   displayWines($result);
   ?>
   </body>
   </html>
        

 

 

There is an .inc file that has all the login information for the db that appears to be working.

 

 

Thanks!

 

 

Link to comment
Share on other sites

   while ($row = @ mysql_fetch_row($result))
       { 
          
          while ($row = @ mysql_fetch_row($result)) {

 

Ouch!

 

Why are you doing this?

 

Cool! thanks! That was it. Just a simple script to access a MySQL db and generate html based on output to html. Unless I am misunderstanding your question. So the real "why" is that I am just wrapping my head around this stuff.

 

Cheers!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.