Search code examples
phppdorowfetchfetchall

PDO error when Fetching results


I am trying to show results from a simple select statement using PDO

<?php  
    // Define and perform the SQL SELECT query
     include('config.inc');
     $user = $_POST['user']; 
     $password = $_POST['password'];


      $sql = "SELECT * FROM usuarios where user = '$user' AND password ='$password'";
      $stm = $db->prepare($sql);
      $stm->execute();
      // here you go:
      $users = $stm->fetchAll();

      foreach ($users as $row) {
           print $row["user"] . "-" . $row["password"] ."<br/>";
      }

    ?>

And the only thing I get is errors like this one:

Undefined index: user in C:\wamp\www\proyect\select.php on line 16

Perhaps is something really simple I might be overlooking in this test, I am working with php 5.3.5.

This is the included file:

  <?php
          $dsn = 'mysql:host=localhost;dbname=carrito';
          $username = 'root';
          $password = 'root';

          try {
               $db = new PDO($dsn, $username, $password);
               $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          }catch (PDOException $e){
               $error_message = $e->getMessage();
               //include('db_error.php');
               echo $error_mesage;
               file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
               exit();
          }
?> 

Solution

  • If I may guess:

    you have PDO::CASE_UPPER set. http://www.php.net/manual/en/pdo.constants.php
    or your column name is just simply upper cased naturally.

    But...stop wondering and start investigating. Simply do

    var_dump($users);
    

    to see what you have.