0

I have this code:

$mysqli = new mysqli('localhost','username','password','database');

$insert = "INSERT INTO submissions ( client_name, client_company ) VALUES ('$client_name', '$client_company' )";

if ( $mysqli->query($insert) === TRUE ) {
  echo "success";
} else {
  echo "Error: " . $mysqli . "<br>" . $mysqli->error;
}

In my development environment it runs perfectly, inserting the variables into the correct table columns.

However in production I get this error:

Catchable fatal error: Object of class mysqli could not be converted to string in 'path' on line x

I know that:

  • There is no error with my mysqli connection as the rest of my application uses the credentials etc. fine to retrieve data from my db in both dev/live
  • That this code runs fine, inserts correctly, doesn't error in my dev environment.

I've looked for solutions to the Error message- and have tried a few, but I remain unconvinced that this is the actual cause for the issue.

  • Wierd the code works on your dev machine... but you need to use '$mysqli->error' instead – Raymond Nijland May 12 '17 at 09:18
  • 2
    I think it's because of the `else` in your code where you try to concatenate the mysqli object into the error message. Try removing that part and see if it works – Florian Humblot May 12 '17 at 09:18
  • @RaymondNijland I've discovered that one of the PHP variables I am passing into the INSERT statement seems to be the cause (the actual statement has about 9 variables, not just the 2 shown). I'm having trouble discerning the exact reason however as that is still fine in dev... – JamieMcGrory May 12 '17 at 10:04

1 Answers1

0

PHP try to cast $mysqli to string here:

echo "Error: " . $mysqli . "<br>" . $mysqli->error;

You should do that like this instead

echo "Error: with mysqli <br>" . $mysqli->error;

Or maybe a var vump if you want more details but mysqli->error should be enough

Bast
  • 369
  • 2
  • 12