如何将mysql扩展转换为mysqli?

时间:2015-08-25 21:08:42

标签: php mysqli

我创建了一个MySQLi连接

$conn = new mysqli($servername, $username, $password, $dbname);

如何将此代码转换为MySQLi

$query  = mysql_query("SELECT * FROM `grid` where user_id = $user_id    and status=0 ORDER BY id ASC");
$count  = mysql_num_rows($query);
if($count > 0) {
    while($fetch = mysql_fetch_array($query)) {
        $record[] = $fetch;
    }
}

1 个答案:

答案 0 :(得分:-1)

$conn = new mysqli($servername, $username, $password, $dbname);

$query = "SELECT * FROM `grid` where user_id = ? AND status=0 ORDER BY id ASC";
$stmt = $conn->prepare($query);
if (!$stmt)
{
    // display error
}

// Bind params
$stmt->bind_param("i", $id);
if (!$stmt->execute())
{
    // your execute error
}

$result = $stmt->get_result();
$data = [];

while ($dataTmp = $result->fetch_assoc())
{
    $data[] = $dataTmp;
}