Problem with PDO Mysql and FETCH::ASSOC
Problem with PDO Mysql and FETCH::ASSOC
am 20.03.2009 00:13:15 von Thomas Robitaille
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
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Problem with PDO Mysql and FETCH::ASSOC
am 20.03.2009 10:44:40 von Goltsios Theodore
> Hello,
>
> I am using the following code to perform queries on a MySQL database:
>
> $dbh = new
> PDO($GLOBALS['database'],$GLOBALS['username'],$GLOBALS['pass word']);
> $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
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Problem with PDO Mysql and FETCH::ASSOC
am 20.03.2009 17:33:45 von Thomas Robitaille
>> 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?
Thanks for your suggestions.
I've managed to fix the issue by switching to a 32-bit installation of
MySQL and Apache. The problem appeared to be due to the 64-bit
versions somehow.
Thomas
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Problem with PDO Mysql and FETCH::ASSOC
am 20.03.2009 17:38:56 von Christopher Jones
Thomas Robitaille wrote:
> I've managed to fix the issue by switching to a 32-bit installation of
> MySQL and Apache. The problem appeared to be due to the 64-bit versions
> somehow.
If you think there's a bug (and you tested the latest version of PHP),
please report the problem at http://bugs.php.net/
Chris
--
Email: christopher.jones@oracle.com Tel: +1 650 506 8630
Twitter: http://twitter.com/ghrd Free PHP Book: http://tinyurl.com/UGPOM
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php