dtqf81594
dtqf81594
2010-07-10 19:47
浏览 38

too long

I started trying to use sqlite to host small websites and on my own personal server, I ran into a glich which kind of spoils the whole idea.

I have a very simple test example, on a two row table in a sqlite 2.x database, I am being hosted with 5.2.12, I have also tried with the PDO sqlite3 database, the problem is this. The "users" table, holds some information, the table is like this

fields(id,username,forename,surname,password)

There are two entries into the table, they are

entry(1,chris,Christopher,Thomas,123) entry(2,adam,Adam,Tanner,456)

The problem is a bit strange, when I query the database like this:

query("select * from users where id=2")

I get the correct results, like this below:

entry(2,adam,Adam,Tanner,456)

When I request ALL the rows, like this query("select * from users") I get this:

entry(1,chris,Christopher,Tanner,456) entry(2,adams,Adamstopher,Tanner,456)

Do you see what seems to have happened? The information in the second entry, is less characters than the first entry, so it seems to be just overwriting the data with the second entry, causing some corruption.

chris adams<---- The s comes from chris

Christopher Adamstopher <--- The stopher comes from christopher


The code is very simple, this is what I run, I try direct sqlite_* calls on sqlite2 and then PDO on sqlite2 and sqlite3 versions of the same database, just to make sure there was any doubt.

(BTW: I added some simple html markup changes and things to make it all look better in the stackoverflow website, those changes aren't in the original code, but they are just things like h1->p or wrapping things with <pre> to preserve the code formatting, etc).

<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");
    }
?>

The output from running this code is:

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
        )
)

If you know why this happens, thanks for letting me know!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandiao3961
    duandiao3961 2010-07-10 20:46
    已采纳
    <?php
    echo PHP_VERSION, ' ', PHP_OS, ' ', PHP_SAPI, "
    ";
    
    $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);
    

    on my machine prints

    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
            )
    
    )
    

    Can you reproduce the erroneous behaviour with this code on your 5.2.x server?

    点赞 评论

相关推荐