invisiblebunnyking Posted January 16, 2009 Share Posted January 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141028-solved-display-mysql-data-in-a-table/ Share on other sites More sharing options...
corbin Posted January 16, 2009 Share Posted January 16, 2009 You shouldn't echo mysql_error unless there is an error. Instead of using the while loop like that, try this instead: while($array = mysql_fetch_array($results)) { } Quote Link to comment https://forums.phpfreaks.com/topic/141028-solved-display-mysql-data-in-a-table/#findComment-738143 Share on other sites More sharing options...
phparray Posted January 16, 2009 Share Posted January 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141028-solved-display-mysql-data-in-a-table/#findComment-738154 Share on other sites More sharing options...
invisiblebunnyking Posted January 16, 2009 Author Share Posted January 16, 2009 Corbin and phparry: Thanks a bunch! It got me on track. phparray's code suggestion did it! Wow, this stuff is easier than asp! Quote Link to comment https://forums.phpfreaks.com/topic/141028-solved-display-mysql-data-in-a-table/#findComment-738157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.