doumen5895 2012-07-03 13:26
浏览 24
已采纳

SQLITE3查询不会在PHP中返回我想要的内容

I open my sq3d fine, but when I try to do a SELECT on one of my tables it seems to not return the results I excpect. Here is the php code:

$buildingArray = array();

class MyDB extends SQLite3
{
function __construct()
{
    $this->open('Database.s3db');
}
}


$db = new MyDB();

$query1 = "SELECT * FROM tbl_uploadData";
$result1 = $db->query($query1);
$u = $result1->fetchArray();
echo "<br/>Size of u: ".sizeOf($u)."<br/>";

for($i=0; $i<sizeOf($u); $i++){
echo "<br/>Items: ".$u[$i]."<br/>";
}

This is what is in my Database:

tbl_uploadData : b_id - 1,2,3,4 : where 1,2,3,4 are the items within the fields, b_id the field name and tbl_uploadData the table.

I expect to get 1, 2, 3 and 4 to return in the fetchArray()

All i get in return is:

Size of u: 2
Items: 1
Notice: Undefined offset: 1 in C:\xampp\htdocs\PHPexcel\Tests\StrategicExcel.php on line 72
Items: 
  • 写回答

2条回答

  • duanhao5038 2012-07-03 13:39
    关注

    Here is the solution, as spaced monkey mentioned, fetchArray() only returns one result at a time. So by adding a while loop and a couple of references to my b_id, it should work. I've modified my code to:

    $databaseName = "Database.s3db";
    
    $db2 = new SQLite3($databaseName); 
        $sql = "SELECT b_id FROM tbl_uploadData"; 
        $result = $db2->query($sql);//->fetchArray(SQLITE3_ASSOC); 
        $row = array(); 
    
        $i = 0; 
         while($res = $result->fetchArray(SQLITE3_ASSOC)){ 
             if(!isset($res['b_id'])) continue; 
                $row[$i]['b_id'] = $res['b_id']; 
                $i++; 
          } 
          print_r($row); 
    

    Hope this helps if anyone comes across this problem in the future, TY

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题