-2

I am getting an error

Fatal error: Uncaught Error: Call to undefined method mysqli::error() in C:\xampp\htdocs\dentalclinic\edit-index.php:83 Stack trace: #0 {main} thrown

At this line

$mysqli->query("DELETE from schedule WHERE patient_id=$patient_id") or die($mysqli->error());

I keep changing the die($mysqli->error()) to this $mysqli->error but I'm getting another error

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near

I also change my code into this

$mysqli->query("DELETE from schedule WHERE patient_id='$patient_id'") or die($mysqli->error); 

Since I notice that I didn't put any single quote in my $patient_id and another error appear that says

Unknown column 'patient_id' in 'where clause'

Below is my whole code

<?php
  $mysqli= new mysqli('localhost','root','','databasename') or 

  die(mysqli_error($mysqli));

  if(isset($_GET['delete'])){

    $mysqli->query("DELETE from schedule WHERE patient_id='$patient_id'") or die($mysqli->error);

  }

My result suppose to be is to delete the row that I'm deleting in my table and will read the patient_id and give its given id.

Dharman
  • 30,962
  • 25
  • 85
  • 135
jevtam_
  • 37
  • 7

2 Answers2

1

You have fallen a victim to misconceptions and lies. Somebody has told you to check for MySQL errors and you tried to use or die($mysqli->error());. In fact this is something which you should NEVER do!

MySQLi can tell you automatically what is wrong if you switch on the error reporting. Just put this line before $mysqli= new mysqli()

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Also, you should never concatenate variables into SQL query. Use prepared statements.

Your fixed code should look like this.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'databasename');
$mysqli->set_charset('utf8mb4');

if (isset($_GET['delete'])) {
    $stmt = $mysqli->prepare('DELETE from schedule WHERE patient_id=?');
    $stmt->bind_param('s', $patient_id);
    $stmt->execute();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

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:

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();

  }
Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • You can't use `$mysqli->error`, because this object doesn't exist yet. Besides what good would that do? You should get warnings on the screen anyway. – Dharman Nov 03 '19 at 17:40
  • @Dharman the answer was showing the OP how to resolve their issue. Which part of my "Fixed Code" is wrong? Also, errors should never be output to screen, that's simply bad practise! – Martin Nov 04 '19 at 10:44
  • I meant that your code has a mistake. I wasn't suggesting to display the error message. If the constructor fails there will be no object to use to read the message from. – Dharman Nov 04 '19 at 11:46
  • The constructor throws warnings one way or the other, so attaching such error handler makes very little sense. If you really wanted to know the error message you can get it from the object after assignment, but through another property called `connect_error` – Dharman Nov 04 '19 at 11:49
  • @Dharman ah ok I see what you're pointing out now, I've updated my final answers. – Martin Nov 04 '19 at 12:28
  • Much better. The prepared statement example should follow the same structure. The object will never be false though, look into PHP docs for the correct examples. – Dharman Nov 04 '19 at 12:30