douweida2878 2017-04-25 08:02
浏览 49

PHP - SQLite意外返回数组结果

First time playing with SQLite, I've created a database and table and set the column id as the primary key unique.

I've inserted some data into the table and can see the data when I do :

SELECT * FROM members;

However when I run the following query I'm not getting the results I expected.

$result = $db->query("SELECT * FROM members WHERE plan = 'User' AND id = '$users_id'");
    echo "<pre>";
    print_r($result->fetchArray());
    echo "</pre>";

I get:

Array
(
    [0] => 124578986532-784512986452
    [id] => 124578986532-784512986452
    [1] => User
    [plan] => User
    [2] => 54890
    [phone] => 54890
    [3] => 698-78450
    [staffID] => 698-78450
    [4] => WestWing
    [location] => WestWing
    [5] => 1
    [active] => 1
)

Which is the correct result, but why do I get duplicates for each returned entry ?

ie: Why [0] & [id], [1] & [plan] ??

As each users ID is unique searching for it will only ever return one result set, how do I use the results as variables I can use elsewhere within the page ?

eg: $id = $result['id'] ?

Thanks

  • 写回答

1条回答 默认 最新

  • duang8642 2017-07-18 06:56
    关注

    fetcharray contains numerical array and associative array, so your result should be

    For Named index array

    $result = $db->query("SELECT * FROM members WHERE plan = 'User' AND id = '$users_id'");
    echo "<pre>";
    print_r($result->fetchArray(PDO::FETCH_ASSOC));
    echo "</pre>";
    

    Here the Crossponding Data

    $result->fetchArray(PDO::FETCH_BOTH) -The rows are arrays with both numeric and named indexes

    $result->fetchArray(PDO::FETCH_ASSOC) -The rows are arrays with named indexes.

    $result->fetchArray(PDO::FETCH_NUM) - The rows are arrays with numeric indexes.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制