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']));

1 comments

@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

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

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:

    error_reporting(E_ALL);
    ini_set('display_errors',1);
    
  • while on a live site, all errors have to be logged, but never shown to the client. For this, configure PHP this way:

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

7 Answered Questions

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

32 Answered Questions

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

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
  • 72154 View
  • 66 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
  • 147 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
  • 278 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
  • 719 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
  • 179 View
  • 1 Score
  • 1 Answer
  • Tags:   pdo sql-injection

Sponsored Content