get_result is only available when php is actually using the msyqlnd driver to connect with the database server. This requires php to be built to use the msyqlnd driver with the MySql database extensions and that the mysqlnd driver is enabled and working. If these conditions are not met, things like mysqliâs get_result, fetch_all, ⌠donât exist and wonât work.
This would be a good time to switch the much simpler and more consistent PDO extension. It has no gotyaâs like this. You would also want to switch to using exceptions for errors and set the default fetch mode to assoc when the database connection is made.
Using PDO, the posted code would simply become -
// note: building your sql query in a php variable aids debugging and reduces typing errors by separating the sql syntax from the php syntax as much as possible
$sql = "SELECT user_id, first_name, last_name, pass FROM users WHERE email=?";
$stmt=$pdo->prepare($sql);
$stmt->execute([$e]);
$row = $stmt->fetch();
Hmm. I may have misunderstood here. I altered the code to:
$stmt=$dbc->prepare("SELECT user_id, first_name, last_name, pass FROM users WHERE email=? ") ;
$stmt->bind_param('s', $e);
$stmt->execute();
if ($stmt->error){echo "something has gone wrong";}
$r=mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ;
**Fatal error** : Uncaught Error: Call to undefined function mysqli_stmt_get_result() in /home/onadbzxi/public_html/rotacreator/login_tools.php:72 Stack trace: #0 /home/onadbzxi/public_html/rotacreator/login_action.php(12): validate() #1 {main} thrown in **/home/onadbzxi/public_html/rotacreator/login_tools.php** on line **72**
(login_tools.php contains a function âvalidateâ.
login_action.php requires login_tools.php.
login.php contains a form whose action is login_action.php)
The use a the ? prepared query place-holder is the same.
Calling the ->prepare() method is the same.
Instead of explicitly using the mysqliâs bind_param() method, you supply an array of the input values to the ->execute([âŚ]) call.
If you are using the last insert id or number of affected rows, there are equivalent PDO calls.
For both a prepared and non-prepared SELECT query, you directly fetch data using either the ->fetch() method (one row), the ->fetchAll() method (all rows), or occasionally the ->fetchColumn() method (a specific column from a row.)
After you have fetched the data into a php variable, the rest of your code using that data should remain the same. If you are not already using this design pattern, which separates your database specific code from your presentation code, this would be a good time to make this change, so that if you ever need to change the database specific code again, you wonât need to touch your presentation code.
The process I adopted to convert to PDO was to create functions, move the existing MySqli script and return a result. I found Database scripts confusing because they can and often return bool results. To simplify the results individual functions were created to accept the SQL statements and return a specific value. Functions were named similar to the following:
getPdoArray($sql);
getPdoObject($sql);
getPdoInteger($sql);
Similar functions created for MySqli scripts.
If you are not aware of the following site related to PDO it is worth bookmarking because it has detailed descriptions and also many good examples.
Alternatively, you could use something like Doctrine DBAL which makes things a bit more consistent and would make it easier to switch from PDO to the next thing, should that ever come along.