Simple php / html Query

powellt77
powellt77 used Ask the Experts™
on
Hi All,

I'm super new to php and would like the answer to what I think would be a simple question.

I've managed to successfully create a working connection string to the mysql database, but I don't know how to display the results. The code (attached) is what I'm trying to achieve; query the table for everything and output into the table. The colum names match the table fields in the database.

Could someone show me how it should look?


<?php

$hostname="x";
$username="x";
$password="x";
$dbname="x";
$usertable="products";

mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);

$query = "Select * from products";

$result = mysql_query($query);

?>

<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled</title>
</head>

<body>

<table width="859" border="1" cellspacing="5" cellpadding="0" align=center>

<tr>
  	<td width="73"><div align="center"><font face="Arial" class="style15"><strong>ID</strong></font></div></td>
	<td width="169"><div align="center"><font face="Arial" class="style15"><strong>ProductType</strong></font></div></td>
    <td width="190"><div align="center"><font face="Arial" class="style15"><strong>ProductName</strong></font></div></td>
    <td width="150"><div align="center"><font face="Arial" class="style15"><strong>SmallPic</strong></font></div></td>
    <td width="110"><div align="center"><font face="Arial" class="style15"><strong>Price</strong></font></div></td>
    <td width="118"><div align="center"><font face="Arial" class="style15"><strong>Active</strong></font></div></td>
  </tr>
<tr>
	<td class="style11"><div align="center"><font face="Arial" size=2></font></div></td>
	<td class="style11"><div align="center"><font face="Arial" size=2></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2></font></div></td>
  </tr>
</table>

</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Marco GasiFreelancer
Top Expert 2010
Commented:
Try this:
 
<?php

$hostname="x";
$username="x";
$password="x";
$dbname="x";
$usertable="products";

mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);

$query = "Select * from products";

$result = mysql_query($query);

?>

<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled</title>
</head>

<body>

<table width="859" border="1" cellspacing="5" cellpadding="0" align=center>
<?php
while ($row = mysql_fetch_assoc($result)){ ?>
<tr>
    <td width="73"><div align="center"><font face="Arial" class="style15"><strong><?php $row['ID'] ?></strong></font></div></td>
    <td width="169"><div align="center"><font face="Arial" class="style15"><strong><?php $row['ProductType'] ?></strong></font></div></td>
    <td width="190"><div align="center"><font face="Arial" class="style15"><strong><?php $row['ProductName'] ?></strong></font></div></td>
    <td width="150"><div align="center"><font face="Arial" class="style15"><strong><?php $row['SmallPic'] ?></strong></font></div></td>
    <td width="110"><div align="center"><font face="Arial" class="style15"><strong><?php $row['Price'] ?></strong></font></div></td>
    <td width="118"><div align="center"><font face="Arial" class="style15"><strong><?php $row['Active'] ?></strong></font></div></td>
</tr>
<?php } ?>
</table>

</body>
</html>

Open in new window


I also suggest a good book to learn how to use php and mysql to build a web applicaiton: http://www.sitepoint.com/books/phpmysql4/

Cheers
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
There are some examples on this page: http://www.php.net/manual/en/function.mysql-fetch-array.php  Basically you create a loop ('while') that echos the data in the proper place in each of the 'td's.

Author

Commented:
Hi Both, thanks for your replies:

marqusG: I tried as posted, but only get 4 blank colums. It's halfway there as there are 4 results, but I just need to see them. See attched
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled</title>
</head>

<body>

<table width="859" border="1" cellspacing="5" cellpadding="0" align=center>
<?php
while ($row = mysql_fetch_assoc($result)){ ?>
<tr>
  	<td width="73"><div align="center"><font face="Arial"><strong>ID</strong></font></div></td>
	<td width="169"><div align="center"><font face="Arial"><strong>ProductType</strong></font></div></td>
    <td width="190"><div align="center"><font face="Arial"><strong>ProductName</strong></font></div></td>
    <td width="150"><div align="center"><font face="Arial"><strong>SmallPic</strong></font></div></td>
    <td width="110"><div align="center"><font face="Arial"><strong>Price</strong></font></div></td>
    <td width="118"><div align="center"><font face="Arial"><strong>Active</strong></font></div></td>
  </tr>
<tr>
	<td class="style11"><div align="center"><font face="Arial" size=2><?php $row['ID'] ?></font></div></td>
	<td class="style11"><div align="center"><font face="Arial" size=2><?php $row['ProductType'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['ProductName'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['SmallPic'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['Price'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['Active'] ?></font></div></td>
  </tr>
<?php } ?>
</table>

</body>
</html>

Open in new window

Capture.JPG

Author

Commented:
Infact - Ignoring my last comment, using this code gives me what I'm looking for (look effect), but without the fields completed
<table width="859" border="1" cellspacing="5" cellpadding="0" align=center>
<tr>
  	<td width="73"><div align="center"><font face="Arial"><strong>ID</strong></font></div></td>
	<td width="169"><div align="center"><font face="Arial"><strong>ProductType</strong></font></div></td>
    <td width="190"><div align="center"><font face="Arial"><strong>ProductName</strong></font></div></td>
    <td width="150"><div align="center"><font face="Arial"><strong>SmallPic</strong></font></div></td>
    <td width="110"><div align="center"><font face="Arial"><strong>Price</strong></font></div></td>
    <td width="118"><div align="center"><font face="Arial"><strong>Active</strong></font></div></td>
  </tr>
<?php
while ($row = mysql_fetch_assoc($result)){ ?>
<tr>
	<td class="style11"><div align="center"><font face="Arial" size=2><?php $row['ID'] ?></font></div></td>
	<td class="style11"><div align="center"><font face="Arial" size=2><?php $row['ProductType'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['ProductName'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['SmallPic'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['Price'] ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php $row['Active'] ?></font></div></td>
  </tr>
<?php } ?>
</table>

Open in new window

You forgot to echo your row values... change your results <tr> to this
<tr>
	<td class="style11"><div align="center"><font face="Arial" size=2><?php echo $row['ID']; ?></font></div></td>
	<td class="style11"><div align="center"><font face="Arial" size=2><?php echo $row['ProductType']; ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php echo $row['ProductName']; ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php echo $row['SmallPic']; ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php echo $row['Price']; ?></font></div></td>
    <td class="style11"><div align="center"><font face="Arial" size=2><?php echo $row['Active']; ?></font></div></td>
  </tr>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial