使用pdo检索值并将它们存储到数组中

时间:2017-07-15 18:10:36

标签: php pdo

我正在尝试从数据库中检索一些值。我检索它们后,我想将它们存储到一个数组中,这个数组通过json发送它。不幸的是,我陷入了错误,我无法找到任何解决方案。 你能帮助我吗?

<?php 
    include_once 'database.php';
    $unique_id = 1;//$_GET['id'];
     try {
     $results = $db->prepare("
        SELECT image_id,image_url,offer_id
        FROM `Images`
        WHERE offer_id = ?");
        $results->bindValue(1, $unique_id);
        $results->execute();
    } catch (Exception $e) {
        echo "Data could not be retrieved from the database.";
        exit;
    }

    $product = $results->fetch(PDO::FETCH_ASSOC);
       if($product['image_url'] != ''){
        $arr="".$product['image_url']."";
        //print_r($arr);
        foreach($arr as $file){ //get an array which has the names of all the files and loop through it 
        $obj['name'] = $file;//get the filename in array
        $obj['size'] = filesize("uploads/".$file); //get the flesize in array
        $result[] = $obj; // copy it to another array
         }
       header('Content-Type: application/json');
       echo json_encode($result); // now you have a json response which you can use in client side 
        }

?>

渲染

<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/Applications/XAMPP/xamppfiles/htdocs/projects/uploads/upload_get.php</b> on line <b>20</b><br />
<br />
<b>Notice</b>:  Undefined variable: result in <b>/Applications/XAMPP/xamppfiles/htdocs/projects/uploads/upload_get.php</b> on line <b>26</b><br />
null

0 个答案:

没有答案