PHP / MYSQLI:在 while 循环之前读取行
PHP / MYSQLI: Read row before while loop
我有以下代码:
<html> //This is using bootstrap, but nevermind, has nothing todo with question...
<body>
<?php
echo "<div class=\"list-group\">".PHP_EOL;
$connection = mysqli_connect('...');
if(mysqli_connect_errno()) {
die('Connection Error');
}
$connection->set_charset("utf8"); //$bid is a var declared before
$getcom = $connection->query("SELECT * FROM hilde_comments WHERE Refer_ID LIKE '$bid' ORDER BY Comment_ID DESC LIMIT 10");
if ($getcom->num_rows > 0) {
while ($row = $getcom->fetch_array(MYSQLI_ASSOC)) {
$getacc = $connection->query("SELECT * FROM hilde_accounts WHERE adress_id LIKE '$row["Account_adress"]'");
if ($getacc->num_rows > 0) {
$accinf = $getacc->fetch_array(MYSQLI_ASSOC);
echo "<div class=\"list-group-item\">".PHP_EOL;
echo "<h4 class=\"list-group-item-heading\"><a href='".$accinf["link"]."' alt=\"Click here!\">".$accinf["email"]."</a></h4>".PHP_EOL;
echo "<p class=\"list-group-item-text\">".$row["Comment"]."</p></div>".PHP_EOL;
} else {
echo "Information ".$row["Account_UID"]." failed.";
}
}
$getcom->close();
$getacc->close();
$connection->close();
echo "</div>";
} else {
$getcom->close();
$connection->close();
echo("<div class=\"list-group-item list-group-item-danger\">Text not found</div>");
}
?>
</body>
</html>
当然,这是行不通的(正如有人在我的 previous question 中指出的那样)。但是为了继续,我需要两个查询的结果来回显,正如您在我的示例片段中看到的那样。如何存储第一个查询的结果并在我的 while 循环中实际将它们用作 ASSOC?
在此先感谢大家,
VicStudio
没有理由不能使用多个查询。您不需要在执行另一个查询之前完成对一个查询的处理。
$getcom = $connection->query("...");
while ($row = $getcom->fetch_array(MYSQLI_ASSOC)) {
$getacc = $connection->query("...");
while ($row2 = $getacc->fetch_array(MYSQLI_ASSOC)) {
echo $row2["Name"]." and ".$row["Adress"];
}
}
编辑现在已经添加了真正的代码,可以看出问题出在这里:
$getacc = $connection->query("SELECT * FROM hilde_accounts WHERE adress_id LIKE '$row["Account_adress"]'");
你在混淆引号。您可能在该查询中遇到了 mysqli 错误(或来自 PHP 的语法错误)。你应该真的为此使用参数化查询,但如果你有以这种方式使用变量,中断你的字符串:
$getacc = $connection->query("SELECT * FROM hilde_accounts WHERE adress_id LIKE '" . $row["Account_adress"] . "'");
只要地址中没有 '
,就可以。请记住,这种代码是等待发生的 SQL 注入攻击,请切换到使用参数化查询。
我有以下代码:
<html> //This is using bootstrap, but nevermind, has nothing todo with question...
<body>
<?php
echo "<div class=\"list-group\">".PHP_EOL;
$connection = mysqli_connect('...');
if(mysqli_connect_errno()) {
die('Connection Error');
}
$connection->set_charset("utf8"); //$bid is a var declared before
$getcom = $connection->query("SELECT * FROM hilde_comments WHERE Refer_ID LIKE '$bid' ORDER BY Comment_ID DESC LIMIT 10");
if ($getcom->num_rows > 0) {
while ($row = $getcom->fetch_array(MYSQLI_ASSOC)) {
$getacc = $connection->query("SELECT * FROM hilde_accounts WHERE adress_id LIKE '$row["Account_adress"]'");
if ($getacc->num_rows > 0) {
$accinf = $getacc->fetch_array(MYSQLI_ASSOC);
echo "<div class=\"list-group-item\">".PHP_EOL;
echo "<h4 class=\"list-group-item-heading\"><a href='".$accinf["link"]."' alt=\"Click here!\">".$accinf["email"]."</a></h4>".PHP_EOL;
echo "<p class=\"list-group-item-text\">".$row["Comment"]."</p></div>".PHP_EOL;
} else {
echo "Information ".$row["Account_UID"]." failed.";
}
}
$getcom->close();
$getacc->close();
$connection->close();
echo "</div>";
} else {
$getcom->close();
$connection->close();
echo("<div class=\"list-group-item list-group-item-danger\">Text not found</div>");
}
?>
</body>
</html>
当然,这是行不通的(正如有人在我的 previous question 中指出的那样)。但是为了继续,我需要两个查询的结果来回显,正如您在我的示例片段中看到的那样。如何存储第一个查询的结果并在我的 while 循环中实际将它们用作 ASSOC?
在此先感谢大家,
VicStudio
没有理由不能使用多个查询。您不需要在执行另一个查询之前完成对一个查询的处理。
$getcom = $connection->query("...");
while ($row = $getcom->fetch_array(MYSQLI_ASSOC)) {
$getacc = $connection->query("...");
while ($row2 = $getacc->fetch_array(MYSQLI_ASSOC)) {
echo $row2["Name"]." and ".$row["Adress"];
}
}
编辑现在已经添加了真正的代码,可以看出问题出在这里:
$getacc = $connection->query("SELECT * FROM hilde_accounts WHERE adress_id LIKE '$row["Account_adress"]'");
你在混淆引号。您可能在该查询中遇到了 mysqli 错误(或来自 PHP 的语法错误)。你应该真的为此使用参数化查询,但如果你有以这种方式使用变量,中断你的字符串:
$getacc = $connection->query("SELECT * FROM hilde_accounts WHERE adress_id LIKE '" . $row["Account_adress"] . "'");
只要地址中没有 '
,就可以。请记住,这种代码是等待发生的 SQL 注入攻击,请切换到使用参数化查询。