Jump to content

PHP - MySQL - Selecting multiple rows from DB


AlwaysDead

Recommended Posts

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

<?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);
?>

<?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)

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

}
?>

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

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.