1

I have a simple code :

$SQL = $db->prepare(
        "INSERT INTO tbl_signup (full_name,email,telephone,password) VALUES (?,?,?,?)");

    $SQL->bind_param('ssss', $full_name,  $signup_email,$telephone, $password);

    $SQL->execute();

This code doesn't work. No row inserted in bdd. No error. Nothing.

And when I add before :

$full_name = $signup_email = $telephone = $password = "";

The code works.

Conclusion : undefined variable are not allowed with bindparam.

But how to get an error in my case ??

I have tried all :

 $SQL->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$SQL->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

 $SQL->execute();

     echo "\nPDO::errorCode(): ", $SQL->errorCode();

      print_r($SQL->errorInfo()); 

     $SQL->debugDumpParams();

But I have no error.

How to get error ?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user2267379
  • 1,067
  • 2
  • 10
  • 20
  • There's a sizable issue here in that your `prepare()` call uses MySQLi methods, but you later attempt to use PDO's error handling. Which one are you using? Their APIs differ. First thing to do is turn on PHP's own error reporting, as it should be complaining about undefined methods. `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your script, always when debugging and testing. A fatal error in the code due to this mismatch, without display_errors enabled is why you see no error reported. – Michael Berkowski Aug 25 '15 at 19:42
  • `bind_param('ssss', $var1, $var2, $varN)` is a MySQLi method. The equivalent in PDO is `bindParam()`. – Michael Berkowski Aug 25 '15 at 19:42
  • Later, if you are truly using PDO rather than MySQLi, the `setAttribute()` would need to be done on the connection object `$db`, not on the statement `$SQL`. – Michael Berkowski Aug 25 '15 at 19:44
  • With `display error 1` all my tentatives to display a mysql error get a error : `Fatal error: Call to undefined method mysqli_stmt::setAttribute()` and again with errorcode... How to display theses mysql error ? – user2267379 Aug 25 '15 at 22:06
  • and if I add only `error_reporting(E_ALL); ini_set('display_errors', 1);` I don't have mysql error and no row is inserted. – user2267379 Aug 25 '15 at 22:13

2 Answers2

1

Your first problem is that you managed to confuse mysqli (which you're actually using) with PDO.

Your second problem is that your conclusion is wrong. Had you running SELECT query, you'd seen that query is running all right.

Your third problem is lack of mysqli error reporting. Add this line before connection:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

and you'll be able to see mysql error converted to PHP exception. Most likely it would be inserting null value when no null allowed

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Yes. That's work. I obtain the desired error : "Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Column 'kbis_url' cannot be null..." Thanks – user2267379 Aug 29 '15 at 14:07
-1

Check there: http://php.net/manual/en/pdo.error-handling.php

and there: http://php.net/manual/en/class.pdoexception.php

Rewrite your code into:

try {
   <..>
   $SQL->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $SQL->execute();
} catch (PDOException $e) {
   echo 'Something wrong: ' . $e->getMessage();
}
Tadas Kvedaras
  • 551
  • 3
  • 9