PHP - select - where statement

Ricky Nguyen
Ricky Nguyen used Ask the Experts™
on
HI experts,

I'm having troubles getting my code to select a specific piece of information.

Here is my code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test 1</title>
</head>
<?php
$con = mysql_connect("localhost","warehouse21","tld3");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("warehouse", $con);

$result = mysql_query("SELECT * FROM product 
WHERE productCode='HT400DVA'");

while($row = mysql_fetch_array($result))
  {
  <?php echo $row['productCode'];?>
  <?php echo $row['name'];?>
  <?php echo $row['price'];?>
 "<br />";
  }
?> 
<?php }
mysql_close($conn); ?>
</table></body></html>

Open in new window


I'm trying to get my code to display the following.

Product Code  |  Name   |   Price
HT400DVA         Bike           $50

Thanks in advance,

Rick
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
F IgorDeveloper

Commented:
Do you want a structured table or simple lines?
First, you are trying to put some php tags within another php code...

//the title line
echo "Product Code  |  Name   |   Price";
echo "<br />";

//the data lines
while($row = mysql_fetch_array($result))
  {
echo $row['productCode'];
echo "|";
echo $row['name'];
echo "|";
echo $row['price'];
echo  "<br />";
  }
you code seams ok, what error do you get?
Top Expert 2004
Commented:
Here are possible corrections (untested). Try and let me know if it fixes things, or if you have any questions:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test 1</title>
</head>
<body>
<table>
  <tr>
    <th>Product Code</th>
    <th>Name</th>
    <th>Price</th>
  </tr>
<?php
$con = mysql_connect("localhost","warehouse21","tld3");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("warehouse", $con);

$result = mysql_query("SELECT * FROM product 
WHERE productCode='HT400DVA'");

while($row = mysql_fetch_array($result))
  {
    echo '<tr>';
    echo '<td>' . $row['productCode'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['price'] . '</td>';
    echo '</tr>';
  }

mysql_close($conn); ?>
</table></body></html>

Open in new window

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Thanks for the fast replies!

@Zyloch

I tried your code and its brought me one step closer I'm getting the following error:

PHP Notice: Undefined variable: conn on line 35 PHP Warning: mysql_close() expects parameter 1 to be resource, null given on line 35

The product code, name and price are showing up now, but that error shows up with them.
You have $conn instead of $con in your mysql_close. Don't give me points for this.

Author

Commented:
Thanks for the help and Thanks for the correction :)

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