Search code examples
phpmysqlselectwhere-in

Select certain ID's from MYSQL and save as PHP Vars


I want to select certain id's from mysql and save the results into associated php vars:

For Example: save COL "Name" from ID "20" to Var: $id20 = "Name";

$queryCS = 'SELECT * FROM ya_events WHERE id IN (20, 16, 21, 37, 40)';

$result = mysql_query($queryCS,$yaDB);
if(mysql_num_rows($result) == '') {'';} else 
{while($row = mysql_fetch_array($result)){

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

}};

echo "ID16: ".$id16;

But $id16 seems to be empty


Solution

  • It's a better idea to use arrays:

    while (...) {
        $ids[$row['id']] = $row['name'];
    }
    
    echo $ids[16];
    

    But, if you really want a bunch of variables lying around:

    ${'id' . $row['id']} = $row['name'];