doure5236 2014-03-06 16:35 采纳率: 0%
浏览 77
已采纳

在PHP中,SQLite3Result对象尚未正确初始化

I have that piece of code in my php file (correctly working):

$db = new Database();
$stmt = $db->prepare("SELECT * FROM bills WHERE group_id=:gid");
$stmt->bindValue(":gid", $_SESSION['group_id'], SQLITE3_INTEGER);
if (($result = $stmt->execute()) === false) {
  echo "bad";
}

$bills = $result;
while ($bill = $bills->fetchArray()) {
  do stuff....
}

Then I try to put all database related code in function:

function getBillsByGID($gid) {
    $db = new Database();
    $stmt = $db->prepare("SELECT * FROM bills WHERE group_id=:gid");
    $stmt->bindValue(":gid", $gid, SQLITE3_INTEGER);

    if (($result = $stmt->execute()) === false) {
        return null;
    }   

    return $result;
}

And in the original file:

$bills = getBillsByGID($_SESSION['group_id']);
while ($bill = $bills->fetchArray()) {
       do stuff...
}

That gives me message: "Warning: SQLite3Result::fetchArray(): The SQLite3Result object has not been correctly initialised in line 61" (line with while($bill = $bills->fetchArray()))

var_dump($bills) after calling the function gives object(SQLite3Result)#10 (0) { }

So how do I make a function which will work properly?

  • 写回答

2条回答 默认 最新

  • dongzhuo5425 2014-03-06 16:43
    关注

    I think you need to fetch data in your function and then return your variable

    function getBillsByGID($gid) {
        $db = new Database();
        $stmt = $db->prepare("SELECT * FROM bills WHERE group_id=:gid");
        $stmt->bindValue(":gid", $gid, SQLITE3_INTEGER);
    
        if ($stmt->execute() === false) {
            return null;
        }   
    
        $array = array();
        while($data = $stmt->fetchArray())
        {
             $array[] = $data;
        }
    
        return $array;
    }
    

    Now assign the function to a variable and loop throw to get your array's data

    $bills = getBillsByGID($_SESSION['group_id']);
    foreach($bills as $data) {
         //do stuff with $data
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?