如何基于sql结果分离php数组?

时间:2013-09-21 10:34:09

标签: php sql arrays comparison

我有一个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列是否为Integer会有所不同吗?

由于

3 个答案:

答案 0 :(得分: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上运行select查询,如果没有返回false,则该值存在于db中,然后您可以将id推送到找到的数组。

这可能有用:How to check if value already exists in MySQL database

答案 1 :(得分: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).")";

答案 2 :(得分: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);

?>