By Rifthy

2015-09-18 09:20:28 8 Comments

This is my PHP sql statement and it's returning false while var dumping

$password_md5 = md5($_GET['password']);
$sql = $dbh->prepare('INSERT INTO users(full_name, e_mail, username, password, password_plain) VALUES (:fullname, :email, :username, :password, :password_plain)');
$result = $sql->execute(array(
                    ':fullname' => $_GET['fullname'], 
                    ':email' => $_GET['email'], 
                    ':username' => $_GET['username'],
                    ':password' => $password_md5,
                    ':password_plain' => $_GET['password']));


@Your Common Sense 2015-09-18 09:22:56

If PDO statement returns FALSE, it means that query failed. You have to set PDO in the proper error reporting mode to be aware of the error.

Put this line in your code right after connect


After getting the error message, you have to read and comprehend it. It sounds too obvious, but learners often overlook the extreme helpfulness of the error message. Yet most of time it explains the problem pretty straightforward. Say, if it says that particular table doesn't exist, you have to check spelling, typos, letter case, credentials and such. Or, if it says there is an error in SQL syntax, then you have to examine your SQL. And the problem spot is right before the query part cited in the error messaage.

You have to also trust the error message. If it says that number of tokens doesn't match number of bound variables then it is so. Same goes for absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.

Note that in order to see PDO errors, you have to be able to see PHP errors in general. To do so, you have to configure PHP depends on the site environment:

  • on a development server it is very handy to have errors right on the screen, for which displaying errors have to be turned on:

  • while on a live site, all errors have to be logged, but never shown to the client. For this, configure PHP this way:

    ini_set('display_errors', 0);
    ini_set('log_errors', 1);

Note that error_reporting should be set to E_ALL all the time.

Also note that despite the common delusion, no try-catch have to be used for the error reporting. PHP will report you PDO errors already, and in a way better form. An uncaught exception is very good for development, yet if you want to show a customized error page, still don't use try catch for this, but just set custom error handler. In a nutshell, you don't have to treat PDO errors as something special but regard them as any other error in your code.

@NaijaProgrammer 2015-09-18 09:25:53

How does that answer the OPs question?

@DTH 2015-09-18 10:16:58

@NaijaProgrammer id doesn't, but it could lead him/her to find the answer based on the error reporting suggested by "Your Common Sense" ..

Related Questions

Sponsored Content

33 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

7 Answered Questions

[SOLVED] Are PDO prepared statements sufficient to prevent SQL injection?

2 Answered Questions

PDO prepared statement insert not working

7 Answered Questions

[SOLVED] PDO::fetchAll vs. PDO::fetch in a loop

  • 2010-05-05 04:31:40
  • Lotus Notes
  • 73931 View
  • 68 Score
  • 7 Answer
  • Tags:   php mysql pdo fetch

1 Answered Questions

PDO prepare statement doesn't substitute placeholders with data

  • 2016-08-17 07:44:46
  • Axiom_shuvo
  • 157 View
  • 0 Score
  • 1 Answer
  • Tags:   php mysql pdo

3 Answered Questions

[SOLVED] How to use SQL prepared statement in php pdo

  • 2016-04-05 16:21:38
  • user2285831
  • 290 View
  • 0 Score
  • 3 Answer
  • Tags:   php mysql pdo

1 Answered Questions

[SOLVED] PDO, multiple statements, and race conditions

1 Answered Questions

[SOLVED] PHP PDO error 1064

  • 2013-04-05 20:40:08
  • Frankrd3
  • 725 View
  • -1 Score
  • 1 Answer
  • Tags:   php mysql pdo

1 Answered Questions

How safe are PDO prepared statement?

  • 2012-08-16 12:25:46
  • Jevgenijs Golojads
  • 184 View
  • 1 Score
  • 1 Answer
  • Tags:   pdo sql-injection

Sponsored Content