Forum Moderators: coopster

Message Too Old, No Replies

weirdness with in array()

         

kristo5747

9:35 pm on May 19, 2010 (gmt 0)

10+ Year Member



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

<?php
//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...

 
...
$customerid = $_POST['customerid'];
$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($customerid, $arr);
//did we return FALSE?.
if ((bool)$result == FALSE) {
echo 'not in array' . (bool)$result;
} else {
echo 'in array' . (bool)$result;
}

...and it does NOT work. 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.

Matthew1980

10:11 pm on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kristo5747,

are you getting anything from this at all?

$result=mysql_query("select distinct customerid from cases where status!='Closed'");
while($row=mysql_fetch_array($result)) {
//$arr[] = $row['customerid'];
print_r($row);
}


Actually see if there is anything coming from the returned array, and if not, there will be a problem with the sql, so add this to the statement:-

$result=mysql_query("SELECT DISTINCT `customerid` FROM `cases` WHERE `status` != 'Closed' ") or die(mysql_error());

Hopefully the connection is being inherited correctly from the previous established connection..

Just to see if there is anything odd from the db side of things...

I altered the formatting of the sql statement but this is just my preference, IMO just makes it easier to interpret.

Also for this:-

if ((bool)$result == FALSE)

You don't really need to typecast the var here as it is only ever going to be a bool returned, an easier way would be if(!$result) that's if I have read this correctly ;)

Cheers,
MRb

kristo5747

10:33 pm on May 19, 2010 (gmt 0)

10+ Year Member



Matthew,

Thanks for your reply.

The solution is simple: I connected to the wrong database

In conclusion, I am an idiot.

Matthew1980

6:54 am on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kristo5747,

Happens to the best of us, at least you have sorted it out now.

Cheers,
MRb