陷入(PHP)foreach mysqli

时间:2018-09-27 12:02:51

标签: php html mysqli foreach

我正在尝试编写一个php页面,以检查select distinct CITY from STATION where ( lower(CITY) not like 'a%' AND lower(CITY) not like 'e%' AND lower(CITY) not like 'i%' AND lower(CITY) not like 'o%' AND lower(CITY) not like 'u%' ) or ( lower(CITY) not like '%a' AND lower(CITY) not like '%e' AND lower(CITY) not like '%i' AND lower(CITY) not like '%o' AND lower(CITY) not like '%u' ); 是否与数据库中的值匹配以及当前访问者的ip地址是否与数据库中存储的var completedList = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 7 }, { id: 8 }]; var invalidList = [{ id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }]; //get the items that are in the invalid list but not completed list var filteredList1 = invalidList.filter((invalidListItem) => !completedList.find((item) => item.id === invalidListItem.id)); //get the items that are in the completed list but not in the invalid list var filteredList2 = completedList.filter((completedListItem) => !invalidList.find((item) => item.id === completedListItem.id)); //join the two arrays var difference = filteredList1.concat(filteredList2); //display the merged array and sort console.log(difference.sort((item1, item2) => { return item1.id > item2.id ? 1 : item1.id < item2.id ? -1 : 0; })); //outputs 1,2,5,6,7,8 的ip地址匹配

代码如下:

$_GET['uuid']

我不知道为什么,但是我陷入了foreach循环。我得到一个带有加载图标的空白页面。

3 个答案:

答案 0 :(得分:1)

您不需要执行任何循环。正如@deceze所说,您可以简单地查询uuid和ip,如果有结果,则表明用户被“允许”。对SQL注入要小心。

  if(!empty($_GET['uuid'])){

    // Evaluate IP and UUID.

    require_once 'database.php';

    if($mysqli->query("SELECT * FROM ips WHERE uuid = $uuid AND ip = $ip")){

      $allowed = 1;

    } else {

      header('Location: index.php');

    }

  } else {

    header('Location: index.php');

  }

还没有测试,但是应该可以。

与您的代码有关的几件事:

  1. 相反,如果包括它,则应使用数据库文件。
  2. 您可以查看PDO和准备好的语句。

答案 1 :(得分:0)

$mysqli->query($sql)返回结果集。

尝试将您的每个foreach循环替换为(针对每个循环进行修改)

$resultSet = $mysqli->query($sql);
while($row = $resultSet->fetch_row())
{
    $ipss = $row;
}

这将检索结果集,然后对返回的每一行执行一个操作。

答案 2 :(得分:0)

您的代码$mysqli->query($sql)返回一个对象,而不是一个可迭代的数组。要获取字段数组,您将需要使用方法fetch_all(MYSQLI_ASSOC)。看下面的例子:

    <?php
 $host = "localhost";
 $mysql = mysqli_connect($host, "root", "", "database");
 $result = $mysql->query("SELECT * FROM table");
 $result = $result->fetch_all(MYSQLI_ASSOC);
 //parameter `MYSQLI_ASSOC` means to return associated array, 
 //(without it you will get a numeric keys for array)
 //$result[0] contains your array.
 //Now you can manipulate data:
 foreach ($result[0] as $field) {
     $get_data = $field['some_col_name'];
 }
 ?>