Re: [PHP-DB] Problem with PDO Mysql and FETCH::ASSOC

[email protected] (Thodoris)
Newsgroups php.db
Message-ID <[email protected]>
> Hello,
>
> I am using the following code to perform queries on a MySQL database:
>
> $dbh = new 
> PDO($GLOBALS['database'],$GLOBALS['username'],$GLOBALS['password']);
> $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
> $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
> $query = $dbh->prepare("SELECT * FROM log");
> $query->execute();
> $results = $query->fetchALL(PDO::FETCH_ASSOC);
> $dbh = null;
>
> If I perform this query in MySQL, I get:
>
> mysql> select * from log;
> +---------+------------+-------------------------------+----------+----------------+----------------------------+ 
>
> | type    | date       | client_id                     | model_id | 
> request        | message                    |
> +---------+------------+-------------------------------+----------+----------------+----------------------------+ 
>
> | message | 1.2375e+09 | domain.18052                  |          | 
> client_start   | started client             |
>
> However, if I perform this query with the PHP code above, I get:
>
> Array ( [type] => error [date] => 1.2375e+09 [log] => [distributed] => 
> client_start [def] => started client) )
>
> which is clearly wrong. 'distributed' is actually the name of the 
> database, so I don't really know what it is doing as a key in the 
> above result.
>
> If I use PDO::FETCH_BOTH instead of PDO::FETCH_ASSOC, I get
>
> Array ( [type] => message [0] => message [date] => 1.2375e+09 [1] => 
> 1.2375e+09 [log] => [2] => domain.18052 [3] => [distributed] => 
> client_start [4] => client_start [def] => started client [5] => 
> started client )
>
> which *does* contain the correct values with the numerical keys. 
> Before using MySQL, I was using SQLite, and this problem did not occur.
>
> The description of the table is:
>
> mysql> describe log;
> +-----------+----------+------+-----+---------+-------+
> | Field     | Type     | Null | Key | Default | Extra |
> +-----------+----------+------+-----+---------+-------+
> | type      | char(10) | YES  |     | NULL    |       |
> | date      | float    | YES  |     | NULL    |       |
> | client_id | char(50) | YES  |     | NULL    |       |
> | model_id  | char(50) | YES  |     | NULL    |       |
> | request   | char(20) | YES  |     | NULL    |       |
> | message   | char(50) | YES  |     | NULL    |       |
> +-----------+----------+------+-----+---------+-------+
> 6 rows in set (0.00 sec)
>
> Does anyone have any ideas as to what I might be doing wrong?
>
> Thanks for any help!
>
> Thomas
>

First of all check if you are actually connecting to the same database 
both times.

Start with printing $GLOBALS['database'] and see if it connects where 
you really want.

You mentioned a field "def" while printing the query's output which 
obviously is not included in the table's description. Have you changed 
the schema in the meantime?

-- 
Thodoris
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.