I am trying to create a function that is able to check an array for a certain value and if that value exists then to return the value of the index.
I have the following code that queries the db and puts the result in an array but when i pass that array into the function nothing is returned for some reason.
Can someone please help me with this.
$conn = mysql_connect($host, $user, $pass);
$conn = mysql_select_db($db, $conn);
$sql="SELECT DATE_FORMAT(due_date,'%d') AS day,short,tid FROM tasks WHERE due_date BETWEEN '2014-8-01' AND '2014-8-31'";
$result=mysql_query($sql);
$num = mysql_num_rows($result);
for ($i = 0; $i < $num; $i++)
{
$events[] = mysql_fetch_assoc($result);
}
var_dump($events);
echo "Check Array results is " . checkArray("15", $events);
function checkArray($day, &$arr){
$key = array_search($day, $arr);
echo "the key is ". $key;
return $key;
}
This is the output of var_dump($events)
array(2) {
[0]=> array(3) {
["day"]=> string(2) "15"
["short"]=> string(6) "test 1"
["tid"]=> string(1) "1"
}
[1]=> array(3) {
["day"]=> string(2) "31"
["short"]=> string(6) "test 2"
["tid"]=> string(1) "2"
}
}
WHERE day = [yourValue]directly in your SQL request ?