1

我有一个 PHP 数据数组(facebook 用户 id),我想将其与存储在我的数据库中的数据(将包含数千个)进行比较。在数据库中找到的那些,我希望它们被分成一个新的数组。我不知道该怎么做。

所以我从这个数组开始,
$ids = 3312312,1232424,1242234,2342636,457456,345345

以两个结束
$found = 34234234,234234234
$notfound = 23234234,234234,23423423

如果有人可以提供帮助,那就太好了。我已经开始了这几种不同的方式,但没有走得很远。理想情况下,我希望一次完成这种比较,但我不确定这是否可能。

谢谢!

编辑

从你所说的,我很快就想出了下面的代码。这似乎是你得到的,但我一直无法慢慢建立到这一点,所以我不确定它是否正确。

<?php
$con=mysqli_connect("localhost","root","","sabotage");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//json array is being posted to this file from another page
$jsonarray = '[{"name":"Lizzie OBrien","id":"218101335"},{"name":"Ellis Ward","id":"512376340"}]';
$friendlist = json_decode($jsonarray, true);

$found = [];
$notfound = [];

foreach($friendlist as $friend){

    $friendid = $friend['id'];
    $checkUserID = mysql_query("SELECT facebookid from users WHERE facebookid = '$friendid'");

    if (!$checkUserID) {
        die('Query failed to execute for some reason');
    }

    if (mysql_num_rows($checkUserId) > 0) {
        $found[] = $id;
    }else{
        $notfound[] = $id;
    }

}


mysqli_close($con);

?>

这给了我:

查询由于某种原因未能执行

我的 facebookid 列是整数是否有区别?

谢谢

4

3 回答 3

1

我会怎么做:

$idsfromdb; //grab all ids from the db, and put in array
$idstobetested; //array of all ids you want to compare
$found = [];
$notfound[];

foreach($idstobetested as $id){
  if(in_array($id, $idsfromdb)){
    $found[] = $id;
  }else{
    $notfound[] = $id;
  }
}

然而:

看到您的评论后,如果您的数据库有大量记录,而不是全部选择并将其放入数组中。相反,遍历您要测试的 id 数组并在 db 上运行选择查询,如果不返回 false,则该值存在于 db 中,然后您可以将 id 推送到找到的数组。

这可能有用:如何检查 MySQL 数据库中是否已存在值

于 2013-09-21T10:44:26.597 回答
0

您可能正在寻找此功能。

$jsonarray = '[{"name":"Lizzie OBrien","id":"218101335"},{"name":"Ellis Ward","id":"512376340"}]';
$friendlist = json_decode($jsonarray, true);

$friendIds = array_map( create_function('$data', 'return $data["id"];'), $friendlist);

// Will return all the matched records
$sql1 = "SELECT yourcolumnname FROM yourtablename WHERE facebookid IN (".implode(',', $friendIds).")";

// Will return all the unmatched records
$sql2 = "SELECT yourcolumnname FROM yourtablename WHERE facebookid NOT IN (".implode(',', $friendIds).")";
于 2013-09-21T11:04:44.120 回答
0

这是为我做的代码。没有你的帮助是不可能做到的。

<?php
$con=mysql_connect("localhost","root","");
// Check connection
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

//json array is being posted to this file from another page
$jsonarray = '[{"name":"Lizzie OBrien","id":"218101335"},{"name":"Ellis Ward","id":"512376340"}]';
$friendlist = json_decode($jsonarray, true);


$found = [];
$notfound = [];

foreach($friendlist as $friend){

    $friendid = $friend['id'];
    mysql_select_db("sabotage", $con);
    $result = mysql_query("SELECT facebookid FROM users WHERE facebookid ='$friendid'", $con);

    if (mysql_num_rows($result) > 0) {
        $found[] = $friendid;
    }else{
        $notfound[] = $friendid;
    }

}

mysql_close($con);

?>
于 2013-09-21T12:54:58.213 回答