-4

i am newbie using this pdo to have access to the database. I am trying to get the ID from the database but I don't receive any data.

1:
$date = array(
            ':user' => $_POST['user'],
            ':pass' => $_POST['pass'],
    );
$user->login($date);

2
function login ($date) {
    $this->dbh->query("Select id From users Where username = ':user' and password = ':pass'");
    $result = $this->dbh->execute_array($date);
    $_SESSION['userid'] = $result['id'];
    //header("Location: game.php");
}

3
function query($query) {
    $this->stmt = $this->db->prepare($query);
}

function execute_array ($array) {
    return $this->stmt->execute($array);
}

edit:

i wrote the pdo code again making some changes that resolve my problem.

function login ($date) {
    $this->dbh->query("Select id From users Where username = :user and password = :pass");
    $this->dbh->execute_array($date);
    $result = $this->dbh->single();
    $_SESSION['userid'] = $result['id'];
    //header("Location: game.php");
}

function execute_array ($array) {
    $this->stmt->execute($array);
}

function single() {
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

ty for helping me with this.

  • 1
    What do you expect to happen, and what is happening now? Do you receive any error messages on-screen or in your logs? – George Cummins Aug 22 '14 at 15:51
  • http://stackoverflow.com/questions/767026/how-can-i-properly-use-a-pdo-object-for-a-select-query – MH2K9 Aug 22 '14 at 15:53
  • You're not actually fetching the results. – Patrick Q Aug 22 '14 at 15:53
  • i dont recive any error..the code runing like the way i want but i dont recive any data..i have this code write with mysql but i'm changing to PDO so i dont know what's is happen..and i think that maybe i have some code missing. – Mário Passos Aug 22 '14 at 15:55
  • Instead of editing your question to contain the answer, you should mark the answer below that you found to be correct or that solved your problem. – Patrick Q Aug 22 '14 at 16:22

2 Answers2

1

You've got sql syntax errors:

[..snip..] username = ':user' and password = ':pass'
                      ^-----^----------------^-----^---here

placeholders do not ever need to be quoted. The database takes care of all that.

Plus, your execute_array() method is probably returning a statement HANDLE. That is not the actual results of your query, merely something you can use to get at those results.

You probably want something more like

   $result = $this->dbh->execute_array(...);
   $row = $result->fetch();
   $_SESSION['userid'] = $row['id'];
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Fatal error: Call to a member function fetch_row() on a non-object – Mário Passos Aug 22 '14 at 16:00
  • i have this code to get single record's: function single() { $this->execute(); return $this->stmt->fetch(PDO::FETCH_ASSOC);} i have to use something like this but the execute method will be with array? – Mário Passos Aug 22 '14 at 16:01
  • whoops, sorry. it's just fetch(), and the error means that the query failed and returned a boolean false anyways. – Marc B Aug 22 '14 at 16:02
0

You prefer it this way:

try{
    $this->db= new PDO(DBHost, DBName, DBUser, DBPassword, array(PDO::ATTR_PERSISTENT => true));
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->db->exec("SET CHARACTER SET utf8");

    $sql="Select id From users Where username = :user and password = :pass";

    $this->stmt = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
    $this->stmt-> bindParam(':user', $date['user'], PDO::PARAM_STR);
    $this->stmt-> bindParam(':pass', $date['pass'], PDO::PARAM_STR);

    $this->stmt->execute();

    if ($row = $stmt->fetch(PDO::FETCH_NAMED, PDO::FETCH_ORI_NEXT)) {
        return $row['id'];
    }else{
        return NULL;
    }
}catch(PDOException $e){
//TODO....
}
Paullo
  • 2,038
  • 4
  • 25
  • 50