Question

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

Was it helpful?

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

OTHER TIPS

while ($row = mysql_fetch_array($result))
{
    $this_variable_name = 'id' . $row['id'];  // $this_variable_name = 'id16';


    // Use PHP variable variables to define
    // and set values for variables like $id16

    $$this_variable_name = $row['name'];
};

echo "ID16: ".$id16;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top