Fatal Error: Call to Undefined Method MySQLi::Error()

PHP: Fatal error: Call to undefined method mysqli

There is your problem:

if ($result = $mysqli->$sql($sql)) {

You're basically calling a method with the same name as your SQL statement, i.e. SELECT tblConference [...]. Such a method doesn't exist, obviously. Rather, try mysqli::query:

if ($result = $mysqli->query($sql)) {

By the way, another error is in this line:

while ($row = $sql->fetch_assoc())

$sql is still your SQL statement. What you want is to iterate over the mysqli_result object, which you've put into the variable $result:

while ($row = $result->fetch_assoc())

What's wrong with my code? I'm having an error to call undefined method mysqli::error()

What is wrong:

Your $mysqli object failed to initialise properly. Most likely due to an authentication or connection issue.

Why is this error not more obvious?

Because you have not set the correct error output OR statement, in your PHP code. You have used the procedural statement on an object instance.

die(mysqli_error($mysqli));

How do I fix this?

You need to read the PHP Manaul for mysqli_error and adjust your code to fit getting an object error message, rather than a *procedural error message. These two styles are not interchangeable on the same variable.

Thus:

die ("DB Error: ". $mysqli->error);

But I still don't get what's wrong?

You need to read your PHP Error log. This will be invaluable in finding and resolving these issues.

If needbe, add the following line to the very top of your PHP page (in development only)

<?php
error_reporting(E_ALL); // add this line. Development only.


Futher suggestions:

  • Use PHP MySQLi Prepared Statements.
  • Tell PHP Not to output errors to the screen, but only to the error log.

Good luck.



Fixed code:

<?php
$mysqli = new mysqli('localhost','root','','databasename');
if (!$mysqli) {
error_log('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
die("A critical error was encountered. Check the logs.");
}

if(isset($_GET['delete'])){
$mysqli->query("DELETE FROM schedule WHERE patient_id='".$patient_id."'") or error_log(print_r($mysqli->error,true));

}

Using Prepared Statements:

<?php
$mysqli= new mysqli('localhost','root','','databasename') or error_log('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
if(!is_object($mysqli)){
die("A critical error was encountered. Check the logs.");
}

if(isset($_GET['delete'])){
$stmt = $mysqli->prepare("DELETE FROM schedule WHERE patient_id=? ") or error_log(print_r($mysqli->error,true));
$stmt->bind_param("i", $patient_id); //maybe s instead of i if a string.
$stmt->execute();

}

PHP MySQL connections error - undefined method mysqli

Your problem is that there is no such function as error() in mysqli. Your code should be:

$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error);

...which accesses the error property (a string) of your $mysqli object. It's not a function.

The reason it's failing in your hosted database is because you're actually getting an error when you try to run your query. If you fix the error-displaying code, you should see the real error.

(You have the bug in your error handling both locally and on your remote machine; it's just that PHP never runs your error-handling code locally because there's no error from $mysqli->query() locally. It's only failing on the remote server.)

Typical reasons for errors after deploying to a different host are things like having the wrong database connection configuration for your hosted database. You'll get more information when you fix the error-checking code.

Uncaught Error: Call to undefined method mysqli_stmt::fetchAll()

This is because there is no such function! You are mixing PDO and mysqli.

If you want to fetch all records from a mysqli prepared statement you need to do it in two steps. First, fetch the result set using mysqli_stmt::get_result() and then use mysqli_result::fetch_all()

$query = "Select * from tblorders";
$stmt = $connection->prepare($query);
$stmt->execute();

$resultSet = $stmt->get_result();
$data = $resultSet->fetch_all(MYSQLI_ASSOC);

However, I would strongly advise learning PDO instead of mysqli as it is much easier and offers more options.

Fatal error: Uncaught Error: Call to undefined method mysqli_result::mysqli_fetch_array() in

From the (very helpful) PHP docs:

$DBConnect->query($sqlGetTop5) returns false if the query failed.
This means something is wrong with your query. Though I am no SQL expert GROUD BY ORDER BY looks a little weird to me.

Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()

You have 2 options.

You can process the resultset from the Statement object like this by binding the columns in the resultset to variables and then use those variables to output the data

<?php
include 'db-conn.php';
$post_username = $_POST['post_username'];
$post_password = $_POST['post_password'];

$sql = "SELECT username, password FROM users WHERE username = ? AND password = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $post_username, $post_password);
$stmt->execute();

// bind variables to the resulted values
$stmt->bind_result($u, $p);

while ($row = $stmt->fetch()) {
echo "Username = $u and Password = $p <br>";
}

Or you can convert the statements resultset to a mysqli_result object and do pretty much what you were doing.

<?php
include 'db-conn.php';
$post_username = $_POST['post_username'];
$post_password = $_POST['post_password'];

$sql = "SELECT username, password FROM users WHERE username = ? AND password = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $post_username, $post_password);
$stmt->execute();

// convert to a result
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
echo "Username = $row[username] and Password = $row[password] <br>";
}


Related Topics



Leave a reply



Submit