Php 为什么可以';我不能正确地回显所有变量吗?

Php 为什么可以';我不能正确地回显所有变量吗?,php,mysql,sql,mysqli,echo,Php,Mysql,Sql,Mysqli,Echo,已编辑,请向下滚动 我试图显示由存储在SQL数据库中的数据组成的3个变量。但是,只有第一个得到了成功的回显(topLeftUrl)。值得注意的是,同一个PHP文件还接收来自输入的数据(也在同一个PHP文件中),并将其存储在同一个SQL数据库中。此代码是为测试目的编写的,可能并不完全安全 //Connect $con = mysqli_connect ("localhost","noneedtoknow","noneedtoknow","noneedtoknow"); if (mysqli_con

已编辑,请向下滚动
我试图显示由存储在SQL数据库中的数据组成的3个变量。但是,只有第一个得到了成功的回显(
topLeftUrl
)。值得注意的是,同一个PHP文件还接收来自输入的数据(也在同一个PHP文件中),并将其存储在同一个SQL数据库中。此代码是为测试目的编写的,可能并不完全安全

//Connect
$con = mysqli_connect ("localhost","noneedtoknow","noneedtoknow","noneedtoknow");
if (mysqli_connect_errno())
{
    echo "Error: ", mysql_connect_error(), "<br>";
    die ();
}

//Store input in SQL database
$result = mysqli_query ($con, "SELECT * FROM edit");
$message = stripslashes ($_POST ['message']);
if ($message !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$message' WHERE id='message'"); }
$topLeftNew = ($_POST ['topLeftUrl']);
if ($topLeftNew !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$topLeftNew' WHERE id='topLeft'"); }
$topRightNew = ($_POST ['topRightUrl']);
if ($topRightNew !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$topRightNew' WHERE id='topRight'"); }

//First echo
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "topLeft" && $done2 == 0)
    {
        $topLeftUrl = $row["cont"];
    }
}
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";

//Second echo
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "topRight" && $done3 == 0)
    {
        $topRightUrl = $row["cont"];
    }
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";

//Third echo
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "message" && $done == 0)
    {
        echo $row["cont"];
    }
}
感谢您的帮助。

编辑:我只需将
mysqli\u data\u seek()
替换为以
$result
开头的行(剪切/粘贴)。谢谢。

我在我的网站上遇到了同样的问题……你在同一个查询($result)上运行了多个mysql\u fetch\u array()。我原以为这在我的网站上会起作用,但除了在我的网站上引用同一查询的6个while循环中的第一个循环外,其余都失败了(很抱歉,我不记得我的错误日志中的确切错误消息)。尝试将3个while循环压缩为1个循环,如下所示:

while ($row = mysqli_fetch_array ($result)) {
    if ($row["id"] == "topLeft" && $done2 == 0) {
        $topLeftUrl = $row["cont"];
    } else if ($row["id"] == "topRight" && $done3 == 0) {
        $topRightUrl = $row["cont"];
    } else if ($row["id"] == "message" && $done == 0) {
        echo $row["cont"];
    } else null;
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";
while($row=mysqli\u fetch\u数组($result)){
如果($row[“id”]==“topLeft”&&$done2==0){
$topLeftUrl=$row[“cont”];
}else if($row[“id”]=“topRight”&&$done3==0){
$topRightUrl=$row[“cont”];
}else if($row[“id”]=“message”&&$done==0){
echo$row[“cont”];
}否则无效;
}
回声“;
回声“;

除非您有单独的/自定义函数,否则语法是而不是
strip\u slashes
。您正在使用
MySQL\u connect\u error()
混合MySQL API-它们不会混合。使用
或die(mysqli_error($con))
是,编辑了stripslashes()。不管怎样,这不是问题所在。您是否尝试在结果集上循环三次?因为您这样做,第二个循环将从第一个循环停止的地方开始。您可以尝试在每个循环之前调用
mysqli\u data\u seek($result,0)
,以重置内部指针。我已经听从了您的建议,但我现在有另一个问题,这在我的问题(我编辑了它)中描述。
while ($row = mysqli_fetch_array ($result)) {
    if ($row["id"] == "topLeft" && $done2 == 0) {
        $topLeftUrl = $row["cont"];
    } else if ($row["id"] == "topRight" && $done3 == 0) {
        $topRightUrl = $row["cont"];
    } else if ($row["id"] == "message" && $done == 0) {
        echo $row["cont"];
    } else null;
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";