mysql results to a html table (mysql_fetch_array)

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

Post Reply
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

mysql results to a html table (mysql_fetch_array)

Post by aussie_clint »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I’m new to php, I’m just trying to get a result from a mysql query into a html table

When I run this script it doesn’t show up a result, not even an error just a blank page, can some one let me know where I’m going wrong?

Thanks
Clint

Code: Select all

<?PHP
//connect to the mysql database
$connection=mysql_connect("localhost","computer_test","test1");
mysql_select_db("computer_price",$connection);
$query="SELECT p.PartNumber, p.Description, (p.IncTax*1.3) AS Price FROM `price list` p";
$data_resource=mysql_query($query,$connection);
//Defining HTML table
$table="<table cellspacing=0 cellpadding=0 width=400>";
      while ($row = mysql_fetch_array($data_resource)) {
$table.="<tr>";
$table.="<td>".$row['p.PartNumber']."</td>";
$table.="<td>".$row['p.Descripton']."</td>";
$table.="</tr>";
      }
//Closing HTML table
$table.="</table>";
//Displying table and closing mysql database if not need 
echo $table;
mysql_close();
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Can you do a right click -> View Source on the blank page, and post the result? Could be that PHP is throwing some error that you can only see in the source of the page.

Could also be that you are getting some sort of database error. You should really have some error handling functionality in your script - it takes 2 minutes extra to write the code, and saves hours and hours of time.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post by panic! »

try print mysql_error(); after the mysql_query.

also this looks quite useful:

http://aidan.dotgeek.org/repos/?file=fu ... 2table.php
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

A blank page means you have a PHP error and your display_error directive is off. Include this at the very beginning of your page...

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>
Be aware that this will only work for non-critical errors. If your page is still blank after adding this piece of code, you will need to scan your code line by line looking for a missing semicolon or some other parse error type (the usual suspects).
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

this is what i get,there is 1331 results,so im getting somet

Post by aussie_clint »

feyd | sorry, had to remove your post.. it was too long.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

I think I've got it...

you are doing

Code: Select all

$table.="<td>".$row['p.PartNumber']."</td>";
try changing to:

Code: Select all

$table.="<td>".$row['PartNumber']."</td>";
cheers,

GM

PS: If you had display errors on, this would throw a notice saying something like "Undefined Index: $row['p.PartNumber']" I think.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Good catch GM. I hadn't even noticed that.
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post by aussie_clint »

the view source is this but where the ... reprecent the same <td></td> times a lot lol


<table cellspacing=0 cellpadding=0 width=400><tr><td></td>...</td></tr></table>

thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

aussie_clint, try GM's suggestion. I think that will fix it for you.
aussie_clint
Forum Commoner
Posts: 41
Joined: Mon Jul 31, 2006 9:14 am
Location: Brisbane, Australia
Contact:

Post by aussie_clint »

sorry for the slow reply, was haveing tuble logging on

it workes fine, thanks heeps guys

clint
Post Reply