Jump to content

Value of a database item without a while loop?


Jaehoon

Recommended Posts

Hey guys, I'm new to PHP so I'm unfamiliar if there is a better way to do this...

 

I have a table in mySQL named "ITEMS" which has items for sale (iphone/blackberry) and some stuff..

 

I'm getting the value by using this method, but I'm thinking is there a better method without using a while loop?

 

$query="SELECT * FROM inventory WHERE id = '$product'";

$result = mysql_query($query,$link);

while($row=@mysql_fetch_array($result))

{

$name = $row["name"];

$item = $row["id"];

$price = $row["price"];

}

 

Is there a way I can directly set a variable in PHP to the product name without the loop?

If you're only returning one row of data from your query, just remove the while-loop outright.  After all, the loop is there to allow you to iterate over several rows of data, not merely one.

 

$query = "SELECT * FROM inventory WHERE id = $product";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$name = $row['name'];
$id = $row['id'];
$price = $row['price'];

Totally off subject here.. but wanted to say..

 

Computer programming is tremendous fun. Like music, it is a skill that derives from an unknown blend of innate talent and constant practice. Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment. Programmers have a well-deserved reputation for working long hours but are rarely credited with being driven by creative fevers. Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination, but because their imagination reveals worlds that others cannot see.

 

I like that..

Archived

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

×
×
  • Create New...

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.