0

I'm getting a blank page on the side where the code is placed to echo data from the database.

My code is:

<?php include 'includes/config2.php' ?>
 <?php $db = new PDO('mysql:host=localhost;dbname=teun_blog', $dbuser,$dbpass);
 $statement = $db->prepare("SELECT products FROM members WHERE memberID = 1");
 $statement->execute(array(':name' => "Teun"));
 $row = $statement->fetch();

?>

 <?php while ($row = $statement->fetch()): ?>
 <tr>
 <td><span class="label label-info">Je hebt <a href="mijnproducten"><?php echo htmlspecialchars($row['products'])?></a> Webhosting pakket(ten).</span></td>
 </tr>
 <?php endwhile; ?>
fredtantini
  • 15,966
  • 8
  • 49
  • 55
Teun Strik
  • 35
  • 7

2 Answers2

2

You are binding a parameter in your execute() statement but there are no parameters in your sql query.

To avoid / easily spot these kinds of problems in the future, you should add error handling. An easy way to do that in PDO, is to have it throw exceptions.

To display errors and have PDO throw exceptions, you can add this to the top of your script:

ini_set('display_errors',1);
error_reporting(E_ALL | E_STRICT);
include 'includes/config2.php';
$db = new PDO('mysql:host=localhost;dbname=teun_blog', $dbuser,
              $dbpass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Not related to the problem at hand, but enabling error reporting at runtime like this won't display any parse errors, and has a habit of getting accidentally checked into source control (and pushed to production). Much better to just change the development machine's php.ini file. – stwalkerster Dec 23 '14 at 13:27
  • @stwalkerster Assuming there is a separate development machine :-) But seriously, I agree but it does display parse errors though. – jeroen Dec 23 '14 at 13:30
  • [A parse error is a fatal, no?](http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors) – stwalkerster Dec 23 '14 at 13:35
  • @stwalkerster Strange, that is not my experience. See for example here (or try it on your own development) and toggle the value between 0 an 1: http://codepad.viper-7.com/jGFKFs – jeroen Dec 23 '14 at 13:47
  • That's a true fatal, not an actual parse error. The php processor could still parse the entire document and execute it. Incidentally, [that codepad has display_errors set to 1](http://codepad.viper-7.com/S08Hz3) in it's php.ini file. A parse error is a true syntax error, [like this](http://codepad.viper-7.com/h7nVS5). Try that on a server with display_errors=0 in the php.ini file. – stwalkerster Dec 23 '14 at 13:53
  • @stwalkerster You're right, I guess I never noticed as I never let it get to that and any decent IDE catches that long before I try to test it :-) – jeroen Dec 23 '14 at 13:57
  • 2
    Seems like the OP's vanished into thin air. Did I hear a *poof?* – Funk Forty Niner Dec 23 '14 at 14:53
1

You try to bind a variable in your prepared statement that was not defined before:

$statement = $db->prepare("SELECT products FROM members WHERE memberID = 1");
$statement->execute(array(':name' => "Teun"));

Fix that and you removed at least one error:

$statement = $db->prepare("SELECT products FROM members WHERE memberID = 1");
$statement->execute();

Your real problem is that you do not get any errors echoed. Look at this question: How to get useful error messages in PHP? To find out how to enable error messages in PHP.

Community
  • 1
  • 1
Johann Bauer
  • 2,488
  • 2
  • 26
  • 41