如何将多个变量设置为各自的值? [关闭]

My sql statement returns an array of 2 associative keys, up and down.

query = 'SELECT `up`,`down` FROM `records` WHERE `id`=id'

is there a way i can, in one line, set both variables such as:

$up, $down = 'SELECT `up`,`down` FROM `records` WHERE `id`=id'

so that up and down will take the respective values?

If after making your SQL call, you get an associative array like this:

array(
  'up' => 'foo',
  'down' => 'bar'
);

Then you can use extract() to get the variables $up and $down.

So you could do this:

// no need to initialize $up and $down

extract(exec_query()); // asusming exec_query() returns the assoc array defined above
echo $up;  // will print 'foo'
echo PHP_EOL;
echo $down;  // will print 'bar'

PS - While this could be done, it's really bad from a readability point of view, and I would strongly recommend against it.

list() allows you to assign multiple values from one function call, however, in your case you also need to execute your query and fetch data from your database server.

For example:

list($up, $down) = mysqli_fetch_array($queryResult, MYSQLI_ASSOC);

The return value from mysqli::query() is not immediately an associative array, but a 'result set'. And if the query fails you will not even get that, but FALSE. So while you could do

list($up, $down) = $db->query ("SELECT up, down FROM records WHERE id='id';")->fetch_all();

this doesn't let you check that the query actually worked, so you're better to split it out.