我的foreach遇到了一些错误。
错误如下:

bool(false) Warning: Invalid argument supplied for foreach() in /storage/content/34/123134

我有bool(false),因为我是var_dump变量。我的代码如下所示:

$getTheOffers = mysql_query("SELECT * FROM `items_offer` , $connen");
foreach ($getTheOffers as $getTheOffer ) { ?>


$ connen是mysql_connection变量。我的选择数据库如下所示:

mysql_select_db($databaseInfo['sqldatabase'],$connen);


怎么了感谢您的支持!

这是完整的foreach和mysql

    $getTheOffers = mysql_query("SELECT * FROM items_offer", $connen);
if( is_array($getTheOffers) )
{
 foreach ($getTheOffers as $getTheOffer ) { ?>
        <tr>
            <td>
                <?php if ($getTheOffer['offer_name'] == "plank") { ?>
                <img src="images/plank.png" alt="Smiley face" height="42" width="42"> (Plank)
                <?php } elseif ($getTheOffer['offer_name'] == "panel") { ?>
                <img src="images/doubleplank.png" alt="Smiley face" height="42" width="42"> (Panel)
                <?php } elseif ($getTheOffer['offer_name'] == "tape") { ?>
                <img src="images/tape.png" alt="Smiley face" height="42" width="42"> (Tape)
                <?php } elseif ($getTheOffer['offer_name'] == "bolt") { ?>
                <img src="images/bolt.png" alt="Smiley face" height="42" width="42"> (Bolt)
                <?php } elseif ($getTheOffer['offer_name'] == "nail") { ?>
                <img src="images/nail.png" alt="Smiley face" height="42" width="42"> (Nail)
                <?php } elseif ($getTheOffer['offer_name'] == "screw") { ?>
                <img src="images/screw.png" alt="Smiley face" height="42" width="42"> (Screw)
                <?php }  ?>
            </td>
            <td><?php echo $getTheOffer['offer_amount'];?></td>
            <td><?php echo $getTheOffer['offer_need'];?></td>
        </tr>
<?php } } else echo "Not work!";  ?>

最佳答案

除了我在注释中指出的缺少代码外,您的mysql_query调用似乎不正确。

尝试这个:

$getTheOffers = mysql_query("SELECT * FROM items_offer", $connen);


还可以尝试这样做以查看查询的情况:

$result = mysql_query("SELECT * FROM items_offer", $connen);
$getTheOffers = mysql_fetch_array($result)
if( is_array($getTheOffers) )
{
    foreach ($getTheOffers as $getTheOffer ) {
        //... rest of your code goes here
    }
} else{
    // output error information
}


有关更多信息,请参考官方文档站点上的mysql_querymysql_fetch_array

关于php - 不同的mysql连接问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20342695/

10-11 00:13