Jump to content


Photo

How can i get requested info from MySQL using php?


  • Please log in to reply
2 replies to this topic

#1 curiosus

curiosus
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 May 2006 - 01:27 PM

I have created a database called 'products'.

A guide adviced to use following script:
INSERT INTO products VALUES(001,"Milk",4.00);
INSERT INTO products VALUES(002,"Chili",5.00);
INSERT INTO products VALUES(003,"Orange",10.00);

I typed the products (product code, product and price) using phpAdmin which my webhost offers.

Then the guide advices me to create producttest.php and add following script into, to print out the info:
<html>
<head>
<title>product test</title>
</head>
<body>

<?php

mysql_connect("localhost","login","password");

mysql_selectdb("shop");

$query = "SELECT code,name,price FROM products ORDER BY code";

$result = mysql_query($query);

?>
<table cellspacing=0 cellpadding=4 border=1>
<tr>
<th>product code</th>
<th>name </th>
<th>price</th>
</tr>
<?
for($count = 0; $count < mysql_numrows($result); $count++) {
?><tr>
<td><?echo mysql_result($result,$count,"code")?></td>
<td><?echo mysql_result($result,$count,"name")?></td>
<td><?echo mysql_result($result,$count,"price")?> USD</td>
</tr>
<?
}
?>
</table>
<?

mysql_close();

?>

</body>
</html>


mysql_connect("localhost","login","password");
I have changed "login" and "password" to my own login and pass. No effect. Then I changed janhost.com as my 'localhost'. No effect.

The results should look like this:
[a href=\"http://imageshack.us\" target=\"_blank\"][img src=\"http://img288.imageshack.us/img288/8918/products6jc.jpg\" border=\"0\" alt=\"IPB Image\" /][/a]
but i'm seeing this:
[a href=\"http://imageshack.us\" target=\"_blank\"][img src=\"http://img74.imageshack.us/img74/973/products29os.jpg\" border=\"0\" alt=\"IPB Image\" /][/a]

Please help me if you can. i've been dealing with this for 3 days...
i've also tried other php codes which should print some info out of MySQL... i can not get them working. Any help will be greatly appreciated!

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 May 2006 - 03:17 PM

Change the below code:
<?
for($count = 0; $count < mysql_numrows($result); $count++) {
?><tr>
<td><?echo mysql_result($result,$count,"code")?></td>
<td><?echo mysql_result($result,$count,"name")?></td>
<td><?echo mysql_result($result,$count,"price")?> USD</td>
</tr>
<?
}
?>
To the following:
<?php 
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['code']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>$<?php echo $row['price']; ?> USD</td>
</tr>
<?php } ?>


#3 curiosus

curiosus
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 May 2006 - 04:41 PM

Thank you for taking time for helping me! it's working now ^^




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users