创建会话时将mysql_result转换为mysqli

时间:2013-04-01 15:25:16

标签: php mysql mysqli

我正在尝试将mysql脚本转换为mysqli并撞墙。我试图将mysql_result转换为mysqli然而我不确定如何做到这一点,下面是我的代码

$_SESSION['num_user'] = mysql_result(mysqli_query($GLOBALS["___mysqli_ston"], "SELECT COUNT(*) FROM `members` WHERE mem_emailactivated = 1"), 0);

3 个答案:

答案 0 :(得分:2)

虽然很奇怪,但似乎没有准备/执行的方法。

$stmt = mysqli_prepare($GLOBALS['___mysqli_ston'], "SELECT COUNT(*) AS count ...");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $count);
mysqli_stmt_fetch($stmt);
$_SESSION['num_user'] = $count;

答案 1 :(得分:0)

你在这里混合了什么。您不会使用mysql_result来使用mysqli从查询中获取结果。您将使用mysqli迭代返回的数组,然后将此数组存储在$_SESSION中。

$result = $mysqli->query("call getUsers()");
if($result){
     // Cycle through results
    while ($row = $result->fetch_object()){
        $user_arr[] = $row;
    }

    $result->close();
}

$_SESSION['num_user'] = $user_arr;

答案 2 :(得分:0)

使用SafeMysql

$sql = "SELECT COUNT(*) FROM `members` WHERE mem_emailactivated = 1";
$_SESSION['num_user'] = $db->getOne($sql);