Pass mysql result to a variable

The below code queries the db and works good.

$getbalance = $SQL->query("SELECT sum(gross_price) FROM `"."my_table` WHERE `id` = '$id'");
while ($row = mysql_fetch_array($getbalance, MYSQL_NUM))
 {
   printf ("%s", $row[0], $row["id"]);
 }

how can I pass this into a single variable or make this into a function and pass that to a variable? I tried but I am just not able so far.

The fact is, you already have your variable. but for some reason getting it most unusual way.

$res = $SQL->query("SELECT sum(gross_price) FROM `my_table` WHERE `id` = '$id'");
$row = mysqli_fetch_num($res);

is all the code you need. $row[0] is already a variable you were looking for.

Where is the value for $id coming from? if it’s supplied by the user then it needs to be validated and a prepared statement used

I tried the code but I wasn’t able to get it to work, id is a column in a table.Thanks for your help

but $id is a variable passed into the query from somewhere else

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.