Page 1 of 1

mysql results to a html table (mysql_fetch_array)

Posted: Mon Jul 31, 2006 9:28 am
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]

Posted: Mon Jul 31, 2006 9:32 am
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.

Posted: Mon Jul 31, 2006 9:44 am
by panic!
try print mysql_error(); after the mysql_query.

also this looks quite useful:

http://aidan.dotgeek.org/repos/?file=fu ... 2table.php

Posted: Mon Jul 31, 2006 9:46 am
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).

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

Posted: Mon Jul 31, 2006 9:47 am
by aussie_clint
feyd | sorry, had to remove your post.. it was too long.

Posted: Mon Jul 31, 2006 9:54 am
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.

Posted: Mon Jul 31, 2006 9:57 am
by RobertGonzalez
Good catch GM. I hadn't even noticed that.

Posted: Mon Jul 31, 2006 9:58 am
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

Posted: Mon Jul 31, 2006 10:00 am
by RobertGonzalez
aussie_clint, try GM's suggestion. I think that will fix it for you.

Posted: Mon Jul 31, 2006 10:24 am
by aussie_clint
sorry for the slow reply, was haveing tuble logging on

it workes fine, thanks heeps guys

clint