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?

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.