所以首先mysql_query被贬值了,你应该看看mysqli_query。但它实际上以正确的方式工作,当执行 SQL 时,如果查询成功,您将返回一个链接标识符。然后,您需要使用例如mysql_fetch_array打开它,因此最终脚本将是:
function get_total_price(){
$result=mysql_query("SELECT SUM(price) FROM db.cart WHERE user_id='".
mysql_real_escape_string($_SESSION['user_id'])."'") or die();
$row= mysql_fetch_array($result);
mysql_free_result($result);//Be a good citizen and release the memory
return $row[0];//The actual sum
}
或者使用较新的库:
//mysqli is a link to the database
function get_total_price($mysqli){
$result=$mysqli->query("SELECT SUM(price) FROM db.cart WHERE user_id='".
$mysqli->real_escape_string($_SESSION['user_id'])."'");
if (!$result) die();//This could be more instructive
$row=$result->fetch_array(MYSQLI_NUM);
$result->free();
return $row[0];//The actual sum
}