tags:

views:

32

answers:

1

Okay I'm trying to convert MySQL request to PDO MySQL. I already do that before but I cn't get this one working :

    $query  = 'SELECT key FROM turl WHERE key = "'.$k.'"';
    $req = $db->query($query);

   if($req->rowCount() > 0) {
        $key = $row['key'];
    }

And here are PDO php extentions :

extension=php_pdo.dll
;extension=php_pdo_dblib.dll
;extension=php_pdo_firebird_firebird.dll
;extension=php_pdo_firebird_interbase.dll
;extension=php_pdo_mssql.dll
;extension=php_pdo_mysql_mysqlnd.dll
extension=php_pdo_mysql_libmysql.dll
extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=php_pdo_sqlite_external.dll

I post here before ServerFault in case it my PHP code that is wrong.

By the way, here is the error :

Fatal error: Call to a member function rowCount() on a non-object in D:\Entreprises\Site web - Dev\xampp\htdocs\ushot\index.php on line 51

+5  A: 

According to the list at http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html - KEY is a reserved mysql keyword.

So you need to quote it with backticks to get valid mysql query

$query  = 'SELECT `key` FROM turl WHERE `key` = "'.$k.'"';

Next: Since you're using PDO - placeholders is more preferred way to insert dynamic values into your queries:

$stmt = $db->prepare('SELECT `key` FROM turl WHERE `key` = :key');
$stmt->execute(array(':key' => 42));
$result = $stmt->fetchAll();
var_dump($result);
zerkms
Wow thank you so much zerkms ! :) I really appreciate! 5/5 ;)
Jeremy Dicaire