PHP SQLite JSON数据重复 [英] PHP SQLite JSON Data Duplication

查看:99
本文介绍了PHP SQLite JSON数据重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP代码:

$testMessage = "TESTMESSAGE";

$db = new SQLite3('messages.sq3');
$db->exec('CREATE TABLE messages(id INTEGER PRIMARY KEY, message CHAR(255));');
$db->exec("INSERT INTO messages (message) VALUES ('$testMessage');");

$results = $db->query('SELECT * FROM messages ORDER BY id DESC LIMIT 5');

while ($row = $results->fetchArray()) {
    print_r($row);
}

生成的print_r:

The resulting print_r:

Array ( [0] => 1 [id] => 1 [1] => TESTMESSAGE [message] => TESTMESSAGE )

为什么要重复这些数据?这只是数组的显示方式还是真的有TESTMESSAGE字符串的两个副本?检查sqlite文件,我只看到一个实际存储在其中的文件.我正在尝试通过JSON对输出进行序列化,而这种重复一直在进行序列化.

Why is this data duplicated? Is this just the way the array is presented or are there really two copies of TESTMESSAGE string? Inspecting the sqlite file, I only see one actually stored there. I am trying to serialize the output via JSON and this duplication is carrying through to the serialization.

推荐答案

默认设置是将具有数字键和字符串键的数据合并到同一数组中.

The default is to have the data with both numeric and string keys, merged in the same array.

您需要使用$results->fetchArray(SQLITE3_NUM)$results->fetchArray(SQLITE3_ASSOC)分别获取数字键和字符串键.默认值为SQLITE3_BOTH,我一直很讨厌.

You need to use $results->fetchArray(SQLITE3_NUM) or $results->fetchArray(SQLITE3_ASSOC) to get numeric and string keys respectively. The default is SQLITE3_BOTH, which I've always hated.

这篇关于PHP SQLite JSON数据重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆