本文介绍了选择带有特殊字符(ó)的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PDO连接ms-access数据库,其中有一个名为Instalación的列:

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());
}

我得到了这样的东西:[Instalaci�n] => DHg我用utf8_encode()解决了它,所以它变成了([Instalación] => DHg)

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

我的问题如下: 当我将字符串查询更改为select Instalación from table时,$db->query($str)返回false

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

我通过更改$str尝试了一些操作,这是相关的错误:

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

错误#1

我做了

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

我遇到的错误:

[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

错误#2

我做了什么:

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

我遇到的错误:

[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] =>

错误#3

我做了什么:

SELECT \'Instalación\' from caudal

我遇到的错误:

[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 wierd that I got rendering problems when displaying errors...

推荐答案

我遇到了同样的错误,我认为您的php源文件是UTF8编码的,并且您正在尝试打开通常与ISO-8859-1一起使用的Access数据库.

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());
    }

这篇关于选择带有特殊字符(ó)的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:15