0

First of I will say I apologize for my title. I don't really know what the correct names of the mysql tables are. So I have a mysql table that looks like the following.

'id'  | 'username' | 'password' | 'product' | 'price'
'1'   | 'ben'      | 'hashedpw' | 'desktop' | '120'
'2'   | 'steve'    | 'hashedpw' | 'laptop'  | '300'

Lets say my database is named hardware and my user for the database is called owner the password is called password and the table is called tech

How would I be able to use PHP to echo the product prices.

$sql = "select * from users";
$query = mysql_query( $sql );

$row['prodyct'];

What I'm having trouble figuring out is how to get the result to show the price of desktop only. I can't use ID since some ID's will be missing at different times.

Ben P. Dorsi-Todaro
  • 221
  • 1
  • 6
  • 20
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Jul 21 '13 at 16:19

3 Answers3

1
SELECT price FROM tech WHERE product = 'desktop';
Hippocrates
  • 2,510
  • 1
  • 19
  • 35
0

This is how you do it:

$sql = "select * from users";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    echo $row['product'];
}

If you want to query for a specific product or something else use on or multiple WHERE conditions:

$sql = "SELECT * FROM users WHERE product LIKE 'desktop'";
Horen
  • 11,184
  • 11
  • 71
  • 113
0
$sql = "select * from users";
$query = mysql_query( $sql );
while ($row = mysql_fetch_array($query , MYSQL_ASSOC)) {
    echo $row[product].':'.$row[price];
}

Please,avoid to use mysql_* functions.

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41