Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于sql结果分离php数组?_Php_Sql_Arrays_Comparison - Fatal编程技术网

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

如何基于sql结果分离php数组?,php,sql,arrays,comparison,Php,Sql,Arrays,Comparison,我有一个PHP数据数组(facebook用户ID),我想将其与存储在数据库中的数据(由数千个组成)进行比较。在数据库中找到的那些数组中,我希望它们被分离到一个新数组中。我不知道该怎么办。 所以我从这个数组开始 $ids=33123121232412422342342636457456345345 以两个结尾 $found=34234 $notfound=2323423 如果有人能帮忙,那就太好了。我已经用了一些不同的方法开始了这项工作,但还没走多远。理想情况下,我希望这个比较能在一次内完成,但

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

所以我从这个数组开始
$ids=33123121232412422342342636457456345345


以两个结尾
$found=34234

$notfound=2323423

如果有人能帮忙,那就太好了。我已经用了一些不同的方法开始了这项工作,但还没走多远。理想情况下,我希望这个比较能在一次内完成,但我不确定这是否可行

谢谢

编辑

根据您所说的,我很快就想出了以下代码。这似乎是你所得到的,但我无法慢慢积累到这一点,所以我不确定这是否正确

<?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);

?>

我该怎么做:

$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推送到找到的数组中


这可能有用:

我该怎么做:

$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推送到找到的数组中


这可能很有用:

您可能正在寻找此函数

$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).")";

您可能正在寻找此函数

$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).")";

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

<?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);

?>

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

<?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);

?>


使用in_array(),但我想从中我需要整个SQL值数组?我很可能会将我的数组(大约500行)与成千上万的sql行进行比较。它们是如何存储在数据库中的?我不确定。你可能知道我不是这方面的专家。我的表有3列,id(主键、自动递增、整数)、name(varchar)和facebookid(int)。我正在看的是facebookid。Cheers使用in_array(),但我想从中我需要整个SQL值数组?我很可能会将我的数组(大约500行)与成千上万的sql行进行比较。它们是如何存储在数据库中的?我不确定。你可能知道我不是这方面的专家。我的表有3列,id(主键、自动递增、整数)、name(varchar)和facebookid(int)。我正在看的是facebookid。干杯,谢谢你。实际上,从您提供的链接来看,我一直无法让代码正常工作。获取“查询由于某种原因无法执行”错误,尽管我确信我的行和表名是正确的。这就是为什么我会这么想,我会重新开始并问:因为我似乎在混合mysqli查询和mysql查询。我会继续调查的。@Theman如果你用你的代码更新你的问题,我可以看一看,如果你愿意的话?我会试试的。这个问题有几个部分,我还没有让它们单独工作,所以可能没有任何意义。给我一分钟,谢谢你。实际上,从您提供的链接来看,我一直无法让代码正常工作。获取“查询由于某种原因无法执行”错误,尽管我确信我的行和表名是正确的。这就是为什么我会这么想,我会重新开始并问:因为我似乎在混合mysqli查询和mysql查询。我会继续调查的。@Theman如果你用你的代码更新你的问题,我可以看一看,如果你愿意的话?我会试试的。这个问题有几个部分,我还没有让它们单独工作,所以可能没有任何意义。给我一分钟。谢谢,但由于一个数组(来自数据库)将有数千个,我不能这样做。根据所有可能的解决方案,您必须从数据库获取所有facebook ID。而不是对所有可以在
query中使用的ID执行查询。我知道我不熟悉这一点,但我认为我不必从数据库中获取所有ID。这将是大量的数据。根据我的最新编辑,我想我需要先检查数据库,然后再对它采取行动。谢谢你,看起来很有希望。我已将此添加到您提供的代码
$result=mysql\u查询($sql1)的末尾;if(!$result){//添加这个check.die('Invalid query:'.mysql_error());}$row=mysql_fetch_数组($result);echo$row['facebookid']但最终查询无效:SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解在第1行的“”附近使用的正确语法。谢谢,但由于一个数组(来自数据库)将有数千个,我不能这样做。根据所有可能的解决方案,您必须从数据库获取所有facebook ID。而不是对所有可以在
query中使用的ID执行查询。我知道我不熟悉这一点,但我认为我不必从数据库中获取所有ID。这将是大量的数据。根据我的最新编辑,我想我需要先检查数据库,然后再对它采取行动。谢谢你,看起来很有希望。我已将此添加到您提供的代码
$result=mysql\u查询($sql1)的末尾;if(!$result){//添加这个check.die('Invalid query:'.mysql_error());}$row=mysql_fetch_数组($result);echo$row['facebookid']但最终查询无效:SQL语法有错误;检查m