2016-07-31 78 views
0

我正在尝试使用PHP检查数组列表中是否存在用户输入值。这里是我的代码:如何检查数组中是否存在一个值使用PHP

$chkvalue=$_POST['id']; 

$sql=$dbh->prepare("my query hear"); 
$sql->execute(); 
$memers=$sql->fetch(PDO::FETCH_BOTH); 

我在阵列$memers我值列表。当我使用var_dump($memers);时,我可以看到数组中的所有值。现在,我想检查天气$chkvalue是否存在于阵列$memers

我尝试下面的代码:

if (in_array($chkvalue ,$memers)) { 
    echo "Value exists"; 
    } else { 
    echo "Value doesn't exists"; 
    } 

但是,它总是显示Value doesn't exists

例如:我的数组包含{} 23568,456982,123489,125895,154879,124648

现在我要检查是否456982中存在数组与否。

我试图FETCH_NUM

我得到以下结果

array(5) { [0]=> string(6) "600258" [1]=> string(15) "A SURYANARAYANA" [2]=> string(6) "420575" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 

array(5) { [0]=> string(6) "223511" [1]=> string(20) "A UMA MAHESWARA RAO" [2]=> string(6) "600258" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 

array(5) { [0]=> string(6) "907774" [1]=> string(19) "A UMA MAHESWARA RAO" [2]=> string(6) "223511" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 

array(5) { [0]=> string(6) "688108" [1]=> string(13) "M BALA BALAJI" [2]=> string(6) "907774" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 
+1

你需要给我们的样本数据与 –

+0

独立问题的工作,虽然,为什么不工作只需在查询中使用'$ chkvalue'? – chris85

+0

$ chkvalue是一个用户输入值 –

回答

1

可能是你,你retrive使用PDO :: FETCH_BOTH所以$ nemers包含结果值的问题,是关系到facth我几个diffrente方式

然后尝试使用

$memers=$sql->fetch(PDO::FETCH_NUM); 

但是,如果你的结果是ñ关联数组,你应该我们somthings这样

<?php 
    $chkvalue=$_POST['id']; 
    $sql=$dbh->prepare("SELECT * FROM ???"); // Put a table name where the ? is. 
    $sql->execute(); 
    while($row = $sql->fetch(PDO::FETCH_ASSOC)) 
    { 
     foreach ($$row as $key => $value) { 
      if ($value == $chkvalue) { 
      echo "Value exists"; 
      } 

     } 
    } 
?> 
+0

我试过同样的问题 –

+0

你确定你获得一个数组而不是一个字符串吗?请更新你的问题,并告诉我var_dump($ memers)的结果; – scaisEdge

+0

我更新了我的问题并添加了样本结果。键值不会与fetch_num一起显示,而是显示键值的fetch_both。我想检查样本结果中的前6位数值。 –

-2

我强烈建议您使用的$stmt->fetch(PDO::FETCH_ASSOC)代替$stmt->fetch(PDO::FETCH_BOTH)。你可以在php或实际的命令中做你想要的。

PHP:

<?php 
$chkvalue=$_POST['id']; 
$sql=$dbh->prepare("SELECT * FROM ???"); // Put a table name where the ? is. 
$sql->execute(); 
while($row = $sql->fetch(PDO::FETCH_ASSOC)) 
{ 
    $lookup_value = '20485930'; 
    if($lookup_value === $row['column_name']) 
    { 
     // Here is a match 
    } 
} 
?> 

SQL与PDO

<?php 
$chkvalue=$_POST['id']; 
$sql=$dbh->prepare("SELECT * FROM ??? WHERE ??? = :value"); // Put a table name 
$sql->bindValue(':value', $value_name, PDO::PARAM_STR); 
// $value_name is a placeholder for the actual value to put here. 
$sql->execute(); 
if($sql->num_rows > 1) 
{ 
    // You have multiple inserts for this value 
} 
else 
{ 
    // You have only one value. 
} 
?> 

希望这有助于。

+3

你不能绑定列/表。 – chris85

+0

为什么不呢?我说要取代?与表名。 – McStuffins

+3

'SELECT * FROM? WHERE:column =:value'无效。 http://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as-parameter – chris85

0

我不明白为什么人们会给予负面投票。总是有一个选择来讨论。怎么过我得到了我的答案in_array剂量不多维数组所以下面的代码工作

function in_array_r($needle, $haystack, $strict = false) { 
    foreach ($haystack as $item) { 
     if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { 
      return true; 
     } 
    } 

    return false; 
} 

和阵列

if(in_array_r($chkvalue, $memers)){ 
     echo 'found'; 
     }else{ 
      echo 'Not Found';  
      } 
相关问题