dpt62283
dpt62283
2015-06-26 15:40

将多个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 dongshi1880 6年前

    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

    点赞 评论 复制链接分享

为你推荐