Jump to content

Archived

This topic is now archived and is closed to further replies.

curiosus

How can i get requested info from MySQL using php?

Recommended Posts

I have created a database called '[b]products[/b]'.

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

I typed the products ([i]product code, product and price[/i]) using [b]phpAdmin[/b] which my webhost offers.

Then the guide advices me to create producttest.php and add following script into, to print out the info:
[code]<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>

[/code]

[code]mysql_connect("localhost","login","password");[/code]
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!

Share this post


Link to post
Share on other sites
Change the below code:
[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>
<?
}
?>[/code]
To the following:
[code]<?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 } ?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.