Return MySQL query as PHP variable

If you have a PHP built webpage and you need to save the MySQL query as a PHP variable, then you can use the following function.
The MySQL query result should be a single entry and it will be saved on the variable when the function is called.

PHP Function

function mysql_get_var($query,$y=0){
$res = mysql_query($query);
$row = mysql_fetch_array($res);
$rec = $row[$y];
return $rec;

You can call the function and set the query you want using the following line. If your query has multiple results, be sure to make the necessary changes on the mysql query such as using the LIMIT command.

PHP Call
$name = mysql_get_var("SELECT name from people where email = '[email protected]'");

Leave a Reply

Your email address will not be published. Required fields are marked *