Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MYSQLI:在while循环之前读取行_Php_Mysqli_Store_Associative Array - Fatal编程技术网

PHP/MYSQLI:在while循环之前读取行

PHP/MYSQLI:在while循环之前读取行,php,mysqli,store,associative-array,Php,Mysqli,Store,Associative Array,我有以下代码: <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()

我有以下代码:

<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>
//这是在使用引导,但不管怎样,与问题无关。。。

没有理由不能使用多个查询。在执行另一个查询之前,不需要完成一个查询的处理

$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注入攻击,请切换到使用参数化查询。

您的第一个查询的结果不是已经在
$row
变量中了吗?@ClémentMalet是的,但是在第一个查询仍然处于活动状态(并且尚未关闭)时,我无法启动第二个查询你能发布你的真实代码吗?我猜您在第二次查询中使用了
$row
,并覆盖了第一次获取。您也可以通过一个查询和一个
连接来完成这一切。打开第二个连接?@chris85现在是真正的代码片段了……什么是参数化查询?谢谢!你帮了我很多忙,不仅在我的问题上,而且在我的网页安全系统上