Jaehoon Posted April 2, 2010 Share Posted April 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/197420-value-of-a-database-item-without-a-while-loop/ Share on other sites More sharing options...
KevinM1 Posted April 3, 2010 Share Posted April 3, 2010 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']; Quote Link to comment https://forums.phpfreaks.com/topic/197420-value-of-a-database-item-without-a-while-loop/#findComment-1036209 Share on other sites More sharing options...
ignace Posted April 3, 2010 Share Posted April 3, 2010 You may also be interested in: $query = 'SELECT field1, field2, field3 FROM table WHERE ..'; $result = mysql_query($query); list($field1, $field2, $field3) = mysql_fetch_row($result); Quote Link to comment https://forums.phpfreaks.com/topic/197420-value-of-a-database-item-without-a-while-loop/#findComment-1036305 Share on other sites More sharing options...
monkeytooth Posted April 3, 2010 Share Posted April 3, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/197420-value-of-a-database-item-without-a-while-loop/#findComment-1036307 Share on other sites More sharing options...
ignace Posted April 3, 2010 Share Posted April 3, 2010 It's from the book Thinking in C# (Larry O'Brien, Bruce Eckel) Quote Link to comment https://forums.phpfreaks.com/topic/197420-value-of-a-database-item-without-a-while-loop/#findComment-1036331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.