문제

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

도움이 되었습니까?

해결책

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

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top