phpms-accesspdo

select column name with special character (ó)


I use PDO to connect a ms-access database where I have a column called Instalación:

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; Dbq=my/path/to/file.mdb");
$str="SELECT * FROM table";
$qr=$db->query($str);
if( $qr != false){
    while($result=$qr->fetch(PDO::FETCH_ASSOC)){
        print_r($result);
    }
}
else{
    print_r($db->errorInfo());
}

I got something like this : [Instalaci�n] => DHg which I solved using utf8_encode() so it became ([Instalación] => DHg)

my problem is the following: when I change the string query to select Instalación from table, the $db->query($str) return false

I tried a few things by changing $str, here are the associated errors:

Error #1

what I did

select Instalación from table or select [Instalación] from table

error I got:

[0] => 07002
[1] => -3010
[2] => [Microsoft][Controlador ODBC Microsoft Access] Pocos par�metros. Se esperaba 1. (SQLExecute[-3010] at ext\pdo_odbc\odbc_stmt.c:254)
[3] => 07002

Error #2

what I did:

$quoted=$db->quote('Instalación');
$str="select $quoted from table";

error I got:

[0] => 42000
[1] => 0
[2] => [Microsoft][Controlador ODBC Microsoft Access] La instrucci�n SELECT incluye una palabra reservada, le falta un argumento o est� mal escrito, o bien los signos de puntuaci�n no son correctos. (SQLPrepare[0] at ext\pdo_odbc\odbc_driver.c:206)
[3] => 

Error #3

what I did:

SELECT \'Instalación\' from caudal

error I got:

[0] => 42000
[1] => 0
[2] => [Microsoft][Controlador ODBC Microsoft Access] Error de sintaxis (falta operador) en la expresi�n de consulta '\'Instalación\''. (SQLPrepare[0] at ext\pdo_odbc\odbc_driver.c:206)
[3] => 

As you can see I ran out of idea to face the problem. Could you help me with that? I also find weird that I got rendering problems when displaying errors...


Solution

  • I got the same error, I think you php source file is UTF8 encoded and you are trying to open a access db that usually works with ISO-8859-1.

    To run your query you must convert your string:

        $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; Dbq=my/path/to/file.mdb");
        //$str="SELECT * FROM table";
    
        $utf8_sql = 'SELECT [Instalación] FROM Table1'; // file must be UTF-8 encoded
        $iso88591_1 = utf8_decode($utf8_sql);
        $iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $utf8_sql);
        $iso88591_sql = mb_convert_encoding($utf8_sql, 'ISO-8859-1', 'UTF-8');
    
    
    
        $qr=$db->query($iso88591_sql);
        if( $qr != false){
            while($result=$qr->fetch(PDO::FETCH_ASSOC)){
                print_r($result);
            }
        }
        else{
            print_r($db->errorInfo());
        }