AlwaysDead Posted November 23, 2008 Share Posted November 23, 2008 Hello everyone, First of all, I have searched a lot for an answer to this, unsuccessful so far. I have found a function that does what I want for 1 row, but I need to select up to 3 rows right now. I am wondering how I can select multiple rows (I think they're called rows? ) from a Database... I want to get the field "ID" from every row that has a specific name... But I want to save those ID numbers in different values. How can I do this the right way? Thanks, AlwaysDead Quote Link to comment https://forums.phpfreaks.com/topic/133903-php-mysql-selecting-multiple-rows-from-db/ Share on other sites More sharing options...
revraz Posted November 23, 2008 Share Posted November 23, 2008 Show us what you are doing now. You just have to modify it and do a while loop to grab them. Quote Link to comment https://forums.phpfreaks.com/topic/133903-php-mysql-selecting-multiple-rows-from-db/#findComment-697024 Share on other sites More sharing options...
flyhoney Posted November 23, 2008 Share Posted November 23, 2008 <?php $query = "SELECT id FROM table WHERE name = 'some name'"; // and if you wanted no more than 3 rows: // $query = "SELECT id FROM table WHERE name = 'some name' LIMIT 3"; // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['id']; } // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133903-php-mysql-selecting-multiple-rows-from-db/#findComment-697026 Share on other sites More sharing options...
AlwaysDead Posted November 23, 2008 Author Share Posted November 23, 2008 <?php $query = "SELECT id FROM table WHERE name = 'some name'"; // and if you wanted no more than 3 rows: // $query = "SELECT id FROM table WHERE name = 'some name' LIMIT 3"; // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['id']; } // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); ?> I can see that it echoes the id, but, uhm, how can I use that ID myself? (in a variable) Quote Link to comment https://forums.phpfreaks.com/topic/133903-php-mysql-selecting-multiple-rows-from-db/#findComment-697051 Share on other sites More sharing options...
flyhoney Posted November 23, 2008 Share Posted November 23, 2008 I echo'd it out, but you can do whatever you want with it. <?php while ($row = mysql_fetch_assoc($result)) { // echo echo $row['id']; // assign $roflcopter = $row['id']; // destroy unset($row['id']); // decorate $row['id'] = '~~.-.-.~~' . $row['id'] . '~~.-.-.~~' ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133903-php-mysql-selecting-multiple-rows-from-db/#findComment-697076 Share on other sites More sharing options...
premiso Posted November 23, 2008 Share Posted November 23, 2008 <?php $query = "SELECT id FROM table WHERE name = 'some name'"; // and if you wanted no more than 3 rows: // $query = "SELECT id FROM table WHERE name = 'some name' LIMIT 3"; // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { $ids[] = $row['id']; } //accesed by foreach ($ids as $id) { echo $id . "<br />"; } // or echo $ids[0]; // to echo the id in position 0. // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result); ?> If you want a better example be more specific on what you would like to do with the id. Quote Link to comment https://forums.phpfreaks.com/topic/133903-php-mysql-selecting-multiple-rows-from-db/#findComment-697078 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.