How can I do the following in mysqli prepare statement?
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
return $data:
Description ¶ Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
The mysql_fetch_assoc() function returns a row from a recordset as an associative array. This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows.
The PHP mysqli_fetch_assoc() function returns an associative array which contains the current row of the result object. This function returns NULL if there are no more rows.
Difference: mysql_fetch_assoc() will always assing a non-secuencial key (like "color" and not a number). mysql_fetch_array() will assing a number key if there are not "word" key (0 and not "color" if there are not "color") but, you can choose to assing of key with a parameter...
You need to use the MySQLi version of the functions:
$result = mysqli_query($sql);
$data = mysqli_fetch_assoc($result);
return $data;
That should do it, you also might want to take a look at:
That's pretty simple after you've created the connection to mysql somewhere:
<?php
// create the connection to mysql.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
?>
The refer to the mysqli object when you need to do a query or do something else.
<?php
// get the result object.
$result = $mysqli->query($sql);
// fetch the result row.
$data = $result->fetch_assoc();
return $data;
?>
Then at some point (if you have a class, you can write a destructor) you should close the connection to mysql if you won't need it anymore.
<?php
$mysqli->kill($mysqli->thread_id);
$mysqli->close();
?>
You can do much more with the result object you have.
So read more about the MySQLi_Result
here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With