Jump to content

weirdness with in_array()


kristo5747

Recommended Posts

Greetings!

 

I have an app that my users rely to update customer data in  our local database. I want to add a "control" whereby a check for customer number is done to make sure it exists.

 

I started with this simple script

//variable declaration
$customerid = '257';
// DEBUG - get type => string
echo gettype($customerid);
//db settings
$dbconf = include '../config/dbConfig.php';
//storage array declaration
$arr = array();
//declaring result as TRUE: we assume all values entered are found in array
$result = TRUE;
//we connect to db host
$con = mysql_connect($dbconf["host"], $dbconf["user"],$dbconf["password"]) ;
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//we connect to database
$db_selected = mysql_select_db($dbconf["db"], $con) or die ("Couldn't select the database.");
if (!$db_selected) {
die ('Could not use '.$dbconf["db"].' : ' . mysql_error());
}
//we get a list of customer ids.
$result=mysql_query("select distinct customerid from cases where status!='Closed'");
while($row=mysql_fetch_array($result)) {
$arr[] = $row['customerid'];
}
// DEBUG - outputs contents => works! Approx. 300+ customer ids
print_r($arr);
// DEBUG - get type => array!
echo gettype($arr);
//we check if input matches any value in array.
(bool)$result = in_array($customerid, $arr);
//we close databse connection and return boolean.
mysql_close($con);

if ((bool)$result == FALSE) {
echo 'not in array';
} else {
echo 'in array';
} 

 

It works perfect. However, I added the same logic to the body of code for my app...

 

    
...
$customer_id = $_POST['case_id'];
$case_status = $_POST['status_change'];

    /*
* Check customerid validity.
    */
    $arr = array();
// DEBUG - get type => string
    echo gettype($customerid);
//we get a list of customerids.
    $result=mysql_query("select distinct customerid from cases where status!='Closed'");
    while($row=mysql_fetch_array($result)) {
        $arr[] = $row['customerid'];
    }
// DEBUG - outputs contents => empty!!
    print_r($arr);
// DEBUG - get type => array
    echo gettype($arr);
//we check if input matches any value in array.
    (bool)$result = in_array($case_id, $arr);
//did we return FALSE?.
    if ((bool)$result == FALSE) {
        echo 'not in array' . (bool)$result;
    } else {
        echo 'in array' . (bool)$result;
    }

...and it does NOTwork. The array is always empty and in_array() always returns FALSE!!!

 

What am I missing? Can someone please tell me! I think I am going nuts.

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/202309-weirdness-with-in_array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.