比较两个数组并在php中打印相同的值。数据库中的数组数据

时间:2015-06-25 05:41:23

标签: php arrays database

我想在php中打印相同的值。这些值来自sql表。

当我运行我的代码时,它显示警告:array_intersect():参数#1不是第69行的C:\ wamp \ www \ programs \ havingpassport_table.php中的数组

这里有什么问题。请告诉我 这是我的代码`

<?php


$conn = new mysqli("localhost","root","","dat_database");



$havepassport='';

$get_emp_id='';

$get_cmp_emp_id='';



$sqlcmp="SELECT EMP_ID from emp_hei WHERE teamleader_id='MMUM253'"; 

$result_cmp = $conn -> query($sqlcmp);

while ($row_cmp = $result_cmp -> fetch_assoc()) {

$get_emp_id = $row_cmp['EMP_ID'];
 //echo $get_emp_id .'<br>';

}


echo '<br>';
$sql_cmp_emp="SELECT EMP_ID from emp_perdetails "; 

$result_cmp_emp = $conn -> query($sql_cmp_emp);

while ($row_cmp_emp = $result_cmp_emp -> fetch_assoc()) {

$get_cmp_emp_id = $row_cmp['EMP_ID'];

  //echo $get_cmp_emp_id .'<br>';
}



$result = array_intersect($get_emp_id, $get_cmp_emp_id);
print_r($result);
?>

`

2 个答案:

答案 0 :(得分:0)

<?php

$conn = new mysqli("localhost","root","","dat_database");

$havepassport='';

$get_emp_id=array();

$get_cmp_emp_id=array();


$sqlcmp="SELECT EMP_ID from emp_hei WHERE teamleader_id='MMUM253'"; 

$result_cmp = $conn -> query($sqlcmp);

while ($row_cmp = $result_cmp -> fetch_assoc()) 
{

$get_emp_id[] = $row_cmp['EMP_ID'];

//echo $get_emp_id .'<br>';

}

echo '<br>';

$sql_cmp_emp="SELECT EMP_ID from emp_perdetails "; 

$result_cmp_emp = $conn -> query($sql_cmp_emp);

while ($row_cmp_emp = $result_cmp_emp -> fetch_assoc()) 
{

$get_cmp_emp_id[] = $row_cmp['EMP_ID'];

//echo $get_cmp_emp_id .'<br>';

}
$result = array_intersect($get_emp_id, $get_cmp_emp_id);

print_r($result);
?>

答案 1 :(得分:0)

你没有保存&#34; to-be-arrays&#34;作为数组。试试这些代码

<?php
//connecting to the database
$conn = new mysqli("localhost","root","","dat_database");

$havepassport='';

//output arrays
$get_emp_id     = [];
$get_cmp_emp_id = [];

//first query to create the $get_emp_id array
$sqlcmp     = "SELECT EMP_ID FROM emp_hei WHERE teamleader_id='MMUM253'"; 
$result_cmp = $conn->query($sqlcmp);

//fetching the EMP_ID into the array
while ($row_cmp = $result_cmp->fetch_assoc()) {
    $get_emp_id[] = $row_cmp['EMP_ID'];
}

//second query to create the $get_cmp_emp_id array
$sql_cmp_emp    ="SELECT EMP_ID FROM emp_perdetails";
$result_cmp_emp = $conn->query($sql_cmp_emp);

while ($row_cmp_emp = $result_cmp_emp->fetch_assoc()) {
    $get_cmp_emp_id[] = $row_cmp['EMP_ID'];
}

//generating the intersect results
$result = array_intersect($get_emp_id, $get_cmp_emp_id);

print_r($result);
?>

如果它没有产生任何结果,请尝试输出每个查询的结果是否返回任何结果。