php - for 循环与 if 语句一起覆盖 PHP

标签 php mysql

我有这段 PHP 代码:

for($i=0;$i<$rowsize;$i++) {

            if($i == $object_id) {

                if ($stmt = $mysqli->prepare('SELECT x, y, src FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE ref_id = ?')) {

                /* Bind parametres */
                $stmt->bind_param('i', $ref_id);

                /* Insert the parameter values */
                $ref_id = $i;

                /* Execute the query */
                $stmt->execute();

                /* Bind resultatet */
                $stmt->bind_result($x, $y, $src);

                /* Close statement */
                $stmt->close();

                } else {
                /* Something went wrong */
                echo 'Something went terrible wrong'     . $mysqli->error;
                }
                echo '<img src="'.$src; echo '"class="item" style="position:relative; left:'.$x; echo 'px; top:'.$y; echo 'px;">';

            }
        }

此代码以 for 循环开始,循环遍历当前包含两行的数据库表。在执行此操作时,它表示如果变量 $i 等于数据库中的对象 ID,则执行此查询以查找 srcxy 来自数据库。之后,它将这些值插入图像中,使图像显示在页面上。然而,即使它必须经过两行,因此必须显示两张图像,但它就像是覆盖了第一张图像,因为只显示了最后一张图像。我认为这与我的 PHP 逻辑有关,但我无法弄清楚如何在不覆盖任何图像的情况下显示这两个图像。提前致谢。

最佳答案

<?php
$stmt = $mysqli->stmt_init();
$stmt->prepare('SELECT x, y, src FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE objects.object_id = ?'); 
$stmt->bind_param('i', $object_id);
if ($stmt->execute()) {
    $stmt->bind_result($x, $y, $src);
    while($stmt->fetch()) {
        echo '<img src="' . $src . '" class="item" style="position:relative; left:' . $x . ' px; top:' . $y . 'px;">';
    }
} else {
    echo 'Something went terrible wrong' . $mysqli->error;
}
$stmt->close();
?>  

关于php - for 循环与 if 语句一起覆盖 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932514/

相关文章:

mysql - 在 MySql 中使用 IN 子句的不同方法

mysql - SQL:Wordpress 用户按最新帖子的日期排序 (CPT)

mysql - 错误代码 : 1060. 不是唯一的表/别名: `table1`

mysql - 如何为MYSQL workbench创建本地数据库?

PHP:可以防止函数回显到屏幕吗?

用于验证 URL 的 PHP 正则表达式

PHP 将键转换为多维数组

php - 数据库查询,根据可更改的选择设置输入元素的值

php - 在 YII 中的一个特定文件中包含核心 CSS

php - 在 MySql 中连接表来存储已查看的文章 ID