0

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();
          }
?> 
Carlos
  • 57
  • 1
  • 16
  • why are you inserting your variables directly, when you are using `prepare()`?? You should be utilizing placeholders, and putting `$user` and `$password` in your `execute()` – Sean Aug 04 '13 at 23:26
  • If you have `Undefined index: user` you not posting a field with name `user`. Are you sure that is your field name? Could it be `username`? To find error is PDO take a look at this answer - http://stackoverflow.com/a/15990858/689579 – Sean Aug 04 '13 at 23:31
  • Didnt I just said it was a test? Its not like im using it in the real world... I just want it to show some results. Of course I know about the parameter binding. – Carlos Aug 04 '13 at 23:32
  • I am sure... I just checked. Also query errors do pop up. And in this case there is no query error. Just the PDO errors. – Carlos Aug 04 '13 at 23:36
  • So what PDO errors are appearing? – Sean Aug 04 '13 at 23:38
  • Change `print $row["user"] . "-" . $row["password"] ."
    ";` to `print_r($row)."
    ";` to see what column names/values are in your table
    – Sean Aug 04 '13 at 23:43

2 Answers2

0

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.

goat
  • 31,486
  • 7
  • 73
  • 96
  • *self-facepalm* Thats the only bloody thing I didnt move in the problem that matters. I was trying... to make it simple so someone... made a constructive comment. Thanks a bunch, you are that person. – Carlos Aug 04 '13 at 23:54
-1

Remove:

$users = $stm->fetchAll();

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

And try this:

while($row = $stm->fetch(PDO::FETCH_ASSOC)){
   print $row["user"] . "-" . $row["password"] ."<br/>";
}
Thelma Eckman
  • 53
  • 1
  • 4
  • No dice... It is annoying because I just did it without PDO and it worked... as soon as I started with PDO this happened. – Carlos Aug 04 '13 at 23:41