简体   繁体   中英

PHP Reference for MySQL query

My function:

   function sql_query($s, $x) {
        $query = mysql_query($s);
        global $mysql;
        while($mysql = mysql_fetch_array($query)) {
        return;
        }
    }

Now it's work only with $mysql variable:

echo $mysql['username'];

How to make it works only with:

sql_query("select * from users where id = '1' limit 1", "varname");
$varname['username'];

I want to set a SQL Query and Variable name in function, like:

sql_query("sqlquery", "variable");
echo $variable['id'];

Thanks for reply!

function sql_query($s, &$x) {
  global $mysql;
  $query = mysql_query($s);
  $result = mysql_fetch_array($query);
  foreach($result as $key => $value) {
    $x[$key] = $value;
  }
}

This should assign each variable that is returned by the query to a key in array $x (assuming it is an array). Notice that I am passing $x by reference instead of by value, eliminating the need to return anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM