0

I've created a MySQLi connection

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

How do convert this code below to 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;
    }
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Johvic1
  • 11
  • 4
  • `MySQLi` has two options: object oriented or procedural. Here your connection part is object oriented which cannot be used with procedural approach (your actual query part). If you use `mysqli_connect` and then just replace `mysql_` in your code with `mysqli_` then that may work. See if this helps: http://php.net/manual/en/book.mysqli.php or this: http://php.net/manual/en/mysqli.quickstart.dual-interface.php – Maximus2012 Aug 25 '15 at 21:11
  • Actually it looks like you can mix the two approaches but I would not recommend that. In your case look at the "Easy migration from the old mysql extension" section here: http://php.net/manual/en/mysqli.quickstart.dual-interface.php. Looks like that is what you're looking for. – Maximus2012 Aug 25 '15 at 21:14

1 Answers1

-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;
}
SarDau Mort
  • 187
  • 4