tags:

views:

33

answers:

2

I needed to change a query into a JOIN of two tables.

$q = "SELECT * FROM table1 AS a JOIN table2 AS b USING(id) WHERE a.id= $id";
$stmt = db::getInstance()->prepare($sql);
return $stmt->fetchAll(PDO::FETCH_ASSOC);

All off the sudden I cannot refer to each row value with $rows['value'] ,BUT I need to use $rows[0]['value'].

How can I avoid this behavior and get the values in the row without using [0]?

Thanks, Richard

A: 

How can I change this to use placeholders?

public function q($sql, $modus = FALSE)
    {   
        try
        {
            $stmt = db::getInstance()->prepare($sql);


        $stmt->execute();
        if( $modus ) return $rst = $modus == "row" ? $stmt->fetch(PDO::FETCH_OBJ) :  $stmt->fetchColumn();
        //else
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    catch(Exception $e)
    {
        $this->fouten[] = $e->getMessage();
    }
}
Richard
This should be a separate question. Also, the manual has full examples: http://www.php.net/manual/en/pdostatement.bindparam.php
Pekka
A: 

If you expect only one row, use PDOStatement::fetch() instead of fetchAll().

Pekka