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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?