dpt62283 2015-06-26 15:40
浏览 60
已采纳

将多个SQL行复制到数组,然后将数组与另一个PHP进行比较

I have a web page that displays a list of names that change throughout the day. I also have a database that contains a list of all of these names.

I need to somehow run a search on the names that are displayed on the web page at any specific time, then match these against the names that are contained in the db. This is sort of a reverse lookup.

The names that are unable to be found on the web page need to be displayed.

How do I go about doing this?

I am attempting to try parsing the names contained in the db rows to an array and the names on the web page to another array then comparing the two arrays.

I have managed to parse them correctly but there is an issue when I try comparing them.

Please point me in the right direction :)

<?php
$a = file_get_contents("https://testpage.com/pbx_info2.php");

#find all the names that are contained within '[' and ']'
preg_match_all('^\[(.*?)\]^', $a, $matches);

#output all the names into an array
$output = $matches[0];

#remove the '[' and ']' characters and print the contents of the array.
foreach($output as $u) {
    $u = str_replace ('[', '', $u);
    $u = str_replace (']', '', $u);
    print $u;
    echo "<br>";
}

$con=mysqli_connect("localhost","test_user","test_pass","test_teaams");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
{
    echo "<br>Connected!<br>";
}

$sql="SELECT * FROM test_table";

if ($result=mysqli_query($con,$sql))
  {
  while ($row=mysqli_fetch_row($result))
    {
    printf (" ".$row[0],$row[1]);
    }
  // Free result set
  $people[0] = mysqli_free_result($result);
  $peep = $people[0];
}

$missing = array_intersect($output, $people);
printf ($missing);

mysqli_close($con);

?>
  • 写回答

1条回答 默认 最新

  • dongshi1880 2015-06-26 15:51
    关注

    If you want to remove the square brackets from your array elements you need to work with references in the foreach loop:

    #remove the '[' and ']' characters and print the contents of the array.
    foreach($output as &$u) {
      $u = str_replace ('[', '', $u);
      $u = str_replace (']', '', $u);
      print $u;
      echo "<br>";
    }
    

    The &$u does the job. Otherwise you will not have changed $u "in place" within its $output array but instead will have created a new $u variable which will be overwritten every time in the loop.

    Instead of comparing the returned results in PHP you could filter the table rows already in the database by creating a suitable WHERE condition for your SELECT statement:

    $where=count($output)?"WHERE usrname NOT IN ('".join("','",$output)).')":'';
    

    This will construct a WHERE clause only when there are userids to be matched. And lateron apply it in

    $sql="SELECT usrname,col1,col2,col3 FROM test_table $where";
    // it is NOT a good idea to use * in SELECT statements since
    // a table structure might change over time ...
    

    assuming that usrname is the column with the user names. The returned rows should be the ones you want: entries in the database that do not appear on the website.

    Edit:

    You can avoid the first problem entirely if you work with a better regular expression:

    preg_match_all('^(?<=\[)(.*?)(?=\])^', $a, $matches);
    

    The (?<=\[) and (?=\]) are called look-behind and look-ahead patterns and are not part of the matched strings.

    see here: https://regex101.com/r/qK4yQ0/1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题