0

I am trying to check if a user input value exists in a array list using PHP. Here is my code:

$chkvalue=$_POST['id'];

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

I got my list of values in array $memers. When I use var_dump($memers); I can see all the values in the array. Now, I want to check weather $chkvalue exists in the array $memers.

I tried the code below:

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

But, it is always showing Value doesn't exists.

For example: my array contains {23568,456982,123489,125895,154879,124648}

Now I want to check if 456982 exists in that array or not.

i tried FETCH_NUM

i got below result

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" } 
Asesha George
  • 2,232
  • 2
  • 32
  • 68
  • 1
    You need to give us sample data to work with – John Conde Jul 31 '16 at 13:32
  • Separate question though, why not just use `$chkvalue` in the query? – chris85 Jul 31 '16 at 13:34
  • $chkvalue is a user input value – Asesha George Jul 31 '16 at 13:35
  • So? You are using prepared statements, bind it. – chris85 Jul 31 '16 at 13:36
  • Also the code and example you provided aren't reproducible. https://eval.in/614718 I thought this code was using `fetchAll` at first which would give a multidimensional array. – chris85 Jul 31 '16 at 13:38
  • `PDO::FETCH_BOTH` will __NEVER__ return `array(23568,456982,123489)`. So `print_r($memers)` and see it's real structure (You can even post it here). – u_mulder Jul 31 '16 at 13:43
  • 1
    All of this is extra work `sql=$dbh->prepare("select 1 from table where column = ?");` then `$sql->execute(array($chkvalue));` if you get a response record/user is in DB already if not then not. If you want specific data put the columns needed in the `select`. – chris85 Jul 31 '16 at 13:45

3 Answers3

1

Could be you issue is related to the facth you retrive the value using PDO::FETCH_BOTH so $nemers contain the result i several diffrente way

then try using

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

But if your result is an associative array you should us somthings like this

<?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";
          }

        }
    }
?>
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

i don't understand why people give negative voting. there is always a option to discuss. how ever i got my answer in_array dose not work with multidimensional array so below code works

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;
}

and for array

if(in_array_r($chkvalue, $memers)){
        echo 'found';
        }else{
            echo 'Not Found';       
            }
Asesha George
  • 2,232
  • 2
  • 32
  • 68
-2

I strongly recommend using $stmt->fetch(PDO::FETCH_ASSOC) instead of $stmt->fetch(PDO::FETCH_BOTH). You could do what you're looking for either in php or in the actual command.

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 with 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.
}
?>

Hope this helps.

Teddy Codes
  • 489
  • 4
  • 14
  • 3
    You cant bind columns/tables. – chris85 Jul 31 '16 at 15:05
  • why not? I said to replace the ? with the table name. – Teddy Codes Jul 31 '16 at 15:30
  • 3
    `SELECT * FROM ? WHERE :column = :value` that is invalid. http://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as-parameter – chris85 Jul 31 '16 at 15:35
  • 1
    With tables/columns?? – chris85 Jul 31 '16 at 15:37
  • without tables. I said in a comment in the code to replace the ? with a table name. for columns I use `bindParam()` instead of `bindValue()`. – Teddy Codes Jul 31 '16 at 15:40
  • Can you provide documentation/code that supports this? In my experience and every thread/manual I've seen that is not the case. I also would distinguish the `?` from the generic placeholder by making it `???`. – chris85 Jul 31 '16 at 15:45
  • I have probably been coding wrong the whole time, but I am on my phone, so I cannot provide anything. When I get to my hotel, I will try. – Teddy Codes Jul 31 '16 at 18:04
  • You have been coding incorrectly and your code cannot be working if [you have been binding the column name as you demonstrate](http://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as-parameter) – Jay Blanchard Aug 03 '16 at 11:47