php sqlite_fetch_array损坏了结果

时间:2010-07-10 19:47:57

标签: php sqlite arrays fetch corrupt

我开始尝试使用sqlite来托管小型网站,而在我自己的个人服务器上,我遇到了一种破坏整个想法的小问题。

我有一个非常简单的测试示例,在sqlite 2.x数据库的两行表中,我正在使用5.2.12托管,我也尝试过使用PDO sqlite3数据库,问题是这个。 “users”表中包含一些信息,表格就是这样的

字段(ID,用户名,名,姓,密码)

表格中有两个条目,它们是

条目(1,克里斯,克里斯托弗,托马斯,123) 条目(2,亚当,亚当,唐纳,456)

当我像这样查询数据库时,问题有点奇怪:

查询(“select * from user where id = 2”)

我得到了正确的结果,如下所示:

条目(2,亚当,亚当,唐纳,456)

当我请求所有行时,例如此查询(“select * from users”),我得到了这个:

条目(1,克里斯,克里斯托弗,唐纳,456) 条目(2,ADAMS,Adamstopher,唐纳,456)

你看到似乎发生了什么吗?第二个条目中的信息比第一个条目的字符少,所以它似乎只是用第二个条目覆盖数据,导致一些损坏。

克里斯 adams< ----来自克里斯

克里斯托弗 Adamstopher< ---阻止来自chri stopher


代码非常简单,这是我运行的,我尝试在sqlite2上直接sqlite_ *调用,然后在同一数据库的sqlite2和sqlite3版本上调试PDO,只是为了确保有任何疑问。

(顺便说一句:我添加了一些简单的html标记更改和事情,使它在stackoverflow网站上看起来更好,这些更改不在原始代码中,但它们只是像h1-> p或包装的东西使用< pre>来保留代码格式等。)

<p>TEST 1 with direct sqlite_* calls</p>
<?php 
try{
    $connection = sqlite_open("../playground.sqlite",0666,$error);
    $handle = sqlite_query("select * from users",$connection);

    $numResults = sqlite_num_rows($handle);

    for($a=0;$a<$numResults;$a++){
        print("<pre>".print_r(sqlite_fetch_array($handle,SQLITE_ASSOC),true)."</pre>");    
    }

}catch(Exception $e){
    die("EXCEPTION OCCURED: '$error'");
}
?>
<p>PDO TEST: SQLITE 2.x</p>
<?php    
    $connection = new PDO('sqlite2:../playground.sqlite');
    $handle = $connection->query("SELECT * FROM users");

    if($handle){
        $result = $handle->fetchAll(PDO::FETCH_ASSOC);
        print("<pre>".print_r($result,true)."</pre>");
    }else{
        var_dump($connection->errorInfo());
        print("query returned negatively");
    }
?>
<p>PDO TEST: SQLITE 3.x</p>
<?php    
    $connection = new PDO('sqlite:../playground.sqlite3');
    $handle = $connection->query("SELECT * FROM users");

    if($handle){
        $result = $handle->fetchAll(PDO::FETCH_ASSOC);
        print("<pre>".print_r($result,true)."</pre>");
    }else{
        var_dump($connection->errorInfo());
        print("query returned negatively");
    }
?>

运行此代码的输出是:

TEST 1 with direct sqlite_* calls
Array
(
    [id] => 1
    [username] => chris
    [forename] => Christopher
    [surname] => Thomas
    [password] => 123
)

Array
(
    [id] => 2
    [username] => adams
    [forename] => Adamstopher
    [surname] => Tanner
    [password] => 456
)

PDO TEST: SQLITE 2.x
Array
(
    [0] => Array
        (
            [id] => 1
            [username] => chris
            [forename] => Christopher
            [surname] => Thomas
            [password] => 123
        )

    [1] => Array
        (
            [id] => 2
            [username] => adams
            [forename] => Adamstopher
            [surname] => Tanner
            [password] => 456
        )

)

PDO TEST: SQLITE 3.x
Array
(
    [0] => Array
        (
            [id] => 1
            [username] => chris
            [forename] => Christopher
            [surname] => Thomas
            [password] => 123
        )

    [1] => Array
        (
            [id] => 2
            [username] => adams
            [forename] => Adamstopher
            [surname] => Tanner
            [password] => 456
        )
)

如果你知道为什么会这样,谢谢你让我知道!

1 个答案:

答案 0 :(得分:2)

<?php
echo PHP_VERSION, ' ', PHP_OS, ' ', PHP_SAPI, "\n";

$pdo = new PDO('sqlite:sotest.sqlite'); 
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$pdo->exec('DROP TABLE IF EXISTS users');
$pdo->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY ASC, username,forename,surname,password)');
$pdo->exec("INSERT INTO users (id,username,forename,surname,password) VALUES (1,'chris','Christopher','Thomas',123)");
$pdo->exec("INSERT INTO users (id,username,forename,surname,password) VALUES (2,'adam','Adam','Tanner',456)");

$rows = $pdo->query("SELECT Length(forename) as cForename, Length(surname) as cSurname, * FROM users")->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);
我机器上的

打印

5.3.2 WINNT cli
Array
(
    [0] => Array
        (
            [cForename] => 11
            [cSurname] => 6
            [id] => 1
            [username] => chris
            [forename] => Christopher
            [surname] => Thomas
            [password] => 123
        )

    [1] => Array
        (
            [cForename] => 4
            [cSurname] => 6
            [id] => 2
            [username] => adam
            [forename] => Adam
            [surname] => Tanner
            [password] => 456
        )

)

您可以在5.2.x服务器上重现此代码的错误行为吗?