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

我打开sq3d很好,但是当我尝试在我的一个表上执行SELECT时,似乎没有返回 我怀疑的结果。 这是php代码:</ p>

  $ buildingArray = array(); 

class MyDB扩展了SQLite3
{
_function __construct()
{
$ this- &gt; open('Database.s3db');
}
}

$ db = new MyDB();

$ query1 =“SELECT * FROM tbl_uploadData”;
$ result1 = $ db-&gt; query($ query1);
$ u = $ result1-&gt; fetchArray();
echo“&lt; br /&gt; u的大小:”。sizeEf($ u)。“&lt; br /&gt;“;

for($ i = 0; $ i&lt; sizeOf($ u); $ i ++){
echo”&lt; br /&gt; Items:“。$ u [$ i]。”&lt ; br /&gt;“;
}
</ code> </ pre>

这是我的数据库中的内容:</ p>

tbl_uploadData:b_id - 1,2,3,4:其中1,2,3,4是字段中的项目,b_id字段名称和tbl_uploadData表格。</ p>

我希望得到1,2 ,3和4在fetchArray()中返回</ p>

我得到的回报是:</ p>

  u的大小:2 
Items :1
通知:未定义的偏移量:在第72行的C:\ xampp \ htdocs \ PHPexcel \ Tests \ StrategicExcel.php中为1
Items:
</ code> </ pre>
</ div>

展开原文

原文

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个回答



这是解决方案,如提到的间隔猴子,fetchArray()一次只返回一个结果。 因此,通过添加while循环和对我的b_id的几个引用,它应该工作。 我已将我的代码修改为:</ p>

  $ databaseName =“Database.s3db”; 

$ db2 = new SQLite3($ databaseName);

$ sql =“SELECT b_id FROM tbl_uploadData”;

$ result = $ db2-&gt; query($ sql); // - &gt; fetchArray(SQLITE3_ASSOC);

$ row = array();

$ i = 0;

while($ res = $ result-&gt; fetchArray(SQLITE3_ASSOC)){
if(!isset($ res ['b_id']))继续;

$ row [$ i] ['b_id'] = $ res ['b_id'];

$ i ++;

}
print_r($ row);

</ code> </ pre>

希望如果有人在将来遇到这个问题,这会有所帮助,TY </ p>
</ div>

展开原文

原文

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



fetchArray()一次只能获得1行,你需要为每一行调用它。 http://uk3.php.net/manual/en/function .sqlite-fetch-array.php </ p>

你应该能够:</ p>

  while($ u = $ result1  - &gt; fetchArray()){
echo“&lt; br /&gt; Items:”。$ u ['b_id']。“&lt; br /&gt;”;
}
</ code> </ pre>

我不知道为什么sizeof($ u)返回2。</ p>
</ div>

展开原文

原文

fetchArray() only gets 1 row at a time, you need to call it for every row. http://uk3.php.net/manual/en/function.sqlite-fetch-array.php

You should be able to:

while ( $u = $result1->fetchArray() ) {
    echo "<br/>Items: ".$u['b_id']."<br/>";
}

I don't know why sizeof($u) is returning 2 though.

dsc862009
dsc862009 我发现了另一种方法,谢谢无论如何,我不知道fetchArray()一次只返回一行所以谢谢!
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐