PHP mysqli查询输出错误

时间:2015-01-17 17:42:01

标签: php mysqli

这已经解决了(编辑在页面的底部)

我在foreach语句中输出查询输出有问题。由于某种原因,转储的转储给出了NULL NULL NULL等,而不是表项的右输出。有谁知道是什么原因引起的?

        <?php
        $offers_array = array();

        $query = "select trade_offer.*, gebruikers.naam, gebruikers.image_url from trade_offer join gebruikers on gebruikers.id = trade_offer.sender_id where receiver_id = ?";
        $stmt = $db->prepare($query);
        $stmt-> bind_param('i', $gebruiker->id); 
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()){
        $offers_array[] = $row; 
        }

        foreach($offers_array as $offer){
        $query_fetch = "select * from items where id in (?,?,?,?,?,?,?,?)";
        $stmt = $db->prepare($query_fetch);
        $stmt-> bind_param('iiiiiiii', $offer['receiver_gets_1'], $offer['receiver_gets_2'], $offer['receiver_gets_3'], $offer['receiver_gets_4'], $offer['sender_gets_1'], $offer['sender_gets_2'], $offer['sender_gets_3'], $offer['sender_gets_4']);
$stmt->execute();
$stmt->bind_result($col1, $col2, $col3, $col4);
    while($stmt->fetch()){
        $id = $col1;
        $naam = $col2;
        $prijs = $col3;
        $image = $col4;

        echo $id;
        echo $naam;
        echo $prijs;
        echo $image;
    }   
}?>

$ offer的转储:

Array
(
    [id] => 8
    [receiver_id] => 2
    [sender_id] => 3
    [receiver_gets_1] => 10
    [receiver_gets_2] => 15
    [receiver_gets_3] => 18
    [receiver_gets_4] => 13
    [sender_gets_1] => 19
    [sender_gets_2] => 14
    [sender_gets_3] => 17
    [sender_gets_4] => 9
    [accepted] => 
    [naam] => John
    [image_url] => http://www.example.com/img.jpg
)

项目内容表:

id   naam  prijs  image_url
1    item1  2.00  example.com
2    item2  2.00  example.com
3    item3  2.00  example.com
4    item4  2.00  example.com
5    item5  2.00  example.com
6    item6  2.00  example.com
etc.

谢谢!

编辑:我将我的代码编辑为Kim Alexander的答案,现在出现以下错误:  Fatal error: Call to a member function bind_param() on boolean in ......\tradeoffers.php on line 31

第31行:

$stmt-> bind_param('iiiiiiii', $offer['receiver_gets_1'], $offer['receiver_gets_2'], $offer['receiver_gets_3'], $offer['receiver_gets_4'], $offer['sender_gets_1'], $offer['sender_gets_2'], $offer['sender_gets_3'], $offer['sender_gets_4']);

1 个答案:

答案 0 :(得分:0)

我认为你的意思不是:

 $result = $stmt->get_result($col1, $col2, $col3, $col4);
    while($stmt->fetch()){
        $id = $col1;
        $naam = $col2;

$stmt->bind_result($col1, $col2, $col3, $col4);
    while($stmt->fetch()){
        $id = $col1;
        $naam = $col2;