Need help with MYSQL connection & displaying output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Need help with MYSQL connection & displaying output

Post by mikosiko »

or using the code that you already have (showing the relevant fragment here and with a very simple example):

Code: Select all

  
.....
// Execute your query (returning a result set in this case
   $sqlresult = mysql_query($sqlquery) or die("Query Error : " . $sqlquery . "<br /> Error: " . mysql_error());

   // Validate that the query returned records
   if (mysql_num_rows($sqlresult) > 0) {
     // Start display of the records
     echo "Business Category : " . $category . "<BR />";
     echo "<table>";
     
     // Loop through your result set to process the results
     while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC)  // here you can simply use mysql_fetch_assoc() instead
     {
        // Process your records here... like
        echo '<tr>';
	echo '<td>' . $row['bizname'] . '</td>';
	echo '<td>' . $row['bizaddress'] . '</td>';
	echo '<td>' . $row['bizcity'] . '</td>';
	echo '<td>' . $row['bizstate'] . '</td>';
	echo '<td>' . $row['bizzip'] . '</td>';
	echo '<td>' . $row['bizphone'] . '</td>';
	echo '<td>' . $row['bizzgooglemap'] . '</td>';
	echo '</tr>';
     }
     echo "</table>";
  } else {
     echo "No records have been found for the category " . $category;
  }
// Close your Db Engine Link
mysql_close($link)
.....
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

Re: Need help with MYSQL connection & displaying output

Post by prototype18 »

Okay, well I got the while{} loop working and it is spewing out multiple records BUT now my problem is that it seems to duplicate certain records in the HTML table... Any idea how to fix that?

Check it out on here http://www.lieconomy.com/bizdir/bizdisplay5.php

Code: Select all

<?php

  // Set the error(s) reporting level
   error_reporting(E_ALL);
   ini_set("display_errors", 1);
   $category = "Professional Services";
   
   // Open a connection link to your Database Engine
   $link = mysql_connect('localhost', 'liecon63_bizdir', 'pass') or die("Connection Error : " . mysql_error());

   // Select the database to work with
   mysql_select_db('liecon63_bizdir') or die("Database Selection Error : " . mysql_error());

   // Define your query
   $sqlquery = "SELECT * FROM bizdir WHERE bizcategory = '".$category."'"; //assuming that you have defined $category variable before in some way

   // Execute your query (returning a result set in this case
   $sqlresult = mysql_query($sqlquery) or die("Query Error : " . $sqlquery . "<br /> Error: " . mysql_error());

   // Loop through your result set to process the results
   while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC))  // here you can simply use mysql_fetch_assoc() instead
   {
       do { ?>
      <table border="0" width="100%" id="table3">
	  <td width="400"><tr>
        <td><?php echo $row['bizname']; ?>
		<br>
		<?php echo $row['bizaddress']; ?>
		<br>
		<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>
		<br>
		<?php echo $row['bizphone']; ?></td>
		<td><?php echo $row['bizname']; ?>
		<br>
		<?php echo $row['bizaddress']; ?>
		<br>
		<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>
		<br>
		<?php echo $row['bizphone']; ?></td>
      </tr>
	  </table>
      <?php } while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC)); 
	  }?>
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Need help with MYSQL connection & displaying output

Post by mikosiko »

just look at the code that I did post for you.... :)

the one that you did is wrong... you have a while inside of the main while... therefore... display your records twice.. sometimes :)
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

Re: Need help with MYSQL connection & displaying output

Post by prototype18 »

mikosiko wrote:just look at the code that I did post for you.... :)

the one that you did is wrong... you have a while inside of the main while... therefore... display your records twice.. sometimes :)
Getting Parse error: syntax error, unexpected '{' in bizdisplay6.php on line 34

Code: Select all

<?php

  // Set the error(s) reporting level
   error_reporting(E_ALL);
   ini_set("display_errors", 1);
   $category = "Professional Services";
   
   // Open a connection link to your Database Engine
   $link = mysql_connect('localhost', 'liecon63_bizdir', 'pass') or die("Connection Error : " . mysql_error());

   // Select the database to work with
   mysql_select_db('liecon63_bizdir') or die("Database Selection Error : " . mysql_error());

   // Define your query
   $sqlquery = "SELECT * FROM bizdir WHERE bizcategory = '".$category."'"; //assuming that you have defined $category variable before in some way

   // Execute your query (returning a result set in this case
   $sqlresult = mysql_query($sqlquery) or die("Query Error : " . $sqlquery . "<br /> Error: " . mysql_error());

   // Validate that the query returned records
   if (mysql_num_rows($sqlresult) > 0) {
     // Start display of the records
     echo "Business Category : " . $category . "<BR />";
     echo "<table>";
     
     // Loop through your result set to process the results
     while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC)  // here you can simply use mysql_fetch_assoc() instead
     {
        // Process your records here... like
        echo '<tr>';
        echo '<td>' . $row['bizname'] . '</td>';
        echo '<td>' . $row['bizaddress'] . '</td>';
        echo '<td>' . $row['bizcity'] . '</td>';
        echo '<td>' . $row['bizstate'] . '</td>';
        echo '<td>' . $row['bizzip'] . '</td>';
        echo '<td>' . $row['bizphone'] . '</td>';
        echo '<td>' . $row['bizzgooglemap'] . '</td>';
        echo '</tr>';
     }
     echo "</table>";
  } else {
     echo "No records have been found for the category " . $category;
  }
// Close your Db Engine Link
mysql_close($link)
?>
</html>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Need help with MYSQL connection & displaying output

Post by John Cartwright »

Your missing a closing bracket on your while statement, and missing a semi colon on your mysql_close statement.

Do not make it a habit of us fixing your parse errors. This could have been easily solved had you looked just a little more carefully. We are here to teach you, not do the work for you ;)
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Need help with MYSQL connection & displaying output

Post by mikosiko »

... at least try to analyze the errors..... you are not using the last code that I did provide to you!!

review the code again (with the modifications that I gave before) and in addition

change this line

Code: Select all

 echo '<td>' . $row['bizzgooglemap'] . '</td>';
for this

Code: Select all

echo '<td>' . $row['bizgooglemap'] . '</td>';

I think that is the right name for your field right?

agree 100% with what John said...
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

Re: Need help with MYSQL connection & displaying output

Post by prototype18 »

Well my editor isnt finding any syntax errors or missing brackets, so im at a loss. :banghead:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Need help with MYSQL connection & displaying output

Post by John Cartwright »

What's your editor got to do with anything?

while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC))

Notice the highlighted bracket, that was missing.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Need help with MYSQL connection & displaying output

Post by mikosiko »

John... those errors where fixed before... and probably he is using again the unfixed code or made a mistake while posting it
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

Re: Need help with MYSQL connection & displaying output

Post by prototype18 »

lol, thanks. I wasn't even paying attention to the parens I was looking for a missing bracket :crazy:

It works now.
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

Re: Need help with MYSQL connection & displaying output

Post by prototype18 »

I'm still having the same problem, I need two columns but the data is repeating in those columns. How can I get the first record the show in the first column cell and then the second record and so on in the second column...?

Code: Select all

   // Define your query
   $sqlquery = "SELECT * FROM bizdir WHERE bizcategory = '".$category."'"; 
   //assuming that you have defined $category variable before in some way

   // Execute your query (returning a result set in this case
   $sqlresult = mysql_query($sqlquery) or die("Query Error : " . $sqlquery . "<br /> Error: " . mysql_error());

   // Validate that the query returned records
   if (mysql_num_rows($sqlresult) > 0) {
     // Start display of the records
     echo "Business Category : " . $category . "<BR />";
     echo "<table>";
     // Loop through your result set to process the results
     while ($row = mysql_fetch_array($sqlresult, MYSQL_ASSOC))  // here you can simply use mysql_fetch_assoc() instead
     {
        // Process your records here... like
?><table border="0" width="100%" id="table3">
	  <td width="400"><tr>
        <td><?php echo $row['bizname']; ?>
		<br>
		<?php echo $row['bizaddress']; ?>
		<br>
		<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>
		<br>
		<?php echo $row['bizphone']; ?></td>
		<td><?php echo $row['bizname']; ?>
		<br>
		<?php echo $row['bizaddress']; ?>
		<br>
		<?php echo $row['bizcity']; ?>, <?php echo $row['bizstate']; ?> <?php echo $row['bizzip']; ?>
		<br>
		<?php echo $row['bizphone']; ?></td>
      </tr>
	  </table>
	  <?
     }
     echo "</table>";
  } else {
     echo "No records have been found for the category " . $category;
  }
// Close your Db Engine Link
mysql_close($link);
?>
</html>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Need help with MYSQL connection & displaying output

Post by John Cartwright »

prototype18 wrote:lol, thanks. I wasn't even paying attention to the parens I was looking for a missing bracket :crazy:

It works now.
Yea it probably would have helped to call the parens by it's formal name. I'm used to just calling them brackets,

I.e.,

Brackets -- ()
Square Brackets -- []
Curly Brackets -- {}
prototype18
Forum Commoner
Posts: 25
Joined: Wed Aug 18, 2010 9:52 am

Re: Need help with MYSQL connection & displaying output

Post by prototype18 »

Well, im very new to this whole thing, so please excuse my ignorance.

But thank you very much for tolerating my insolence.
Post Reply