Jump to content

[SOLVED] Display MySQL data in a table


invisiblebunnyking

Recommended Posts

Hi all!

I'm migrating my site from .asp to php, which is interesting since I'm a noob to php. Things have been going ok, but now I'm stumped. All my data for the site is in a mysql database. I can connect to the database, but can not get the data to display on my pages. The problem is when i try to repeat the rows of the table to show all items. There is something with the $i=0 part, but can't figure it out. Following is the code:

 

<?php

require_once('connection.php');

 

mysql_connect($dbhost, $dbuser, $dbpassword);

mysql_select_db($dbname) or die("PHAIL");

 

$query = "SELECT * FROM bigcardtable WHERE category='GameUsed' ORDER BY 'PlayerName';";

 

$results = mysql_query($query);

echo mysql_error();

$array = mysql_fetch_array($results);

$num_Rows = mysql_num_rows($results);

 

$i=0;

while($i <= num_Rows ) {

$row = $array[$i];

echo '<table width="100%" border="1">';

echo '  <tr>';

echo '    <td rowspan="6"><img src="' . $row['ImagePath'] . '" /></td>';

echo '    <td>Player Name</td>';

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

echo '  </tr>';

echo '  <tr>';

echo '    <td>Card Description</td>';

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

echo '  </tr>';

echo '  <tr>';

echo '    <td>Serial Number</td>';

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

echo '  </tr>';

echo '  <tr>';

echo '    <td>Price</td>';

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

echo '  </tr>';

echo '  <tr>';

echo '    <td>Purchase</td>';

echo '    <td> </td>';

echo '  </tr>';

echo '  <tr>';

echo '    <td colspan="2"> </td>';

echo '  </tr>';

echo '</table>';

$i++;

}

?>

 

I hope I have provided enough information for people. Any help would be appreciated!

Thanks in advance.

Clint

Link to comment
https://forums.phpfreaks.com/topic/141028-solved-display-mysql-data-in-a-table/
Share on other sites

corbin is exactly correct here.  For the sake of clarity I use

 

while($row = mysql_fetch_array($results)) {


}

 

This way you can get rid of the  $row = $array[$i]; line totally and not change the rest of your variable names. You can lose $i = 0; too.  You don't need an accumulator here because this usage will continue the while loop until there are no more rows.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.