Avatar of powellt77
powellt77
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Simple php / html Query

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

PHP

Avatar of undefined
Last Comment
PranjalShah

8/22/2022 - Mon
SOLUTION
Marco Gasi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Dave Baldwin

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.
powellt77

ASKER
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
powellt77

ASKER
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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
PranjalShah

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question