junkk rr
junkk rr

Reputation: 55

mysqli_query not working inside array_map function

Mysqli_query is not working inside array_map function. My code is :

define('DB_SERVER','localhost');
define('DB_USERNAME','xxx');
define('DB_PASSWORD','yyy');
define('DB_NAME','fff');

$conn = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);

foreach($unique_array as $supplier) 
{
     $urep[]=$supplier['rep_name'];
     $udate[]=$supplier['date'];
$ucid[]=$supplier['cid'];

$unique_arrayyy[] = array('rep_name'=>$supplier['rep_name'], 'date'=>$supplier['date']);
}

array_map(function ($var) {
$fetch_gdd=mysqli_query($conn,"select * from grade");

echo mysqli_num_rows($fetch_gdd); exit;

}, $unique_arrayyy);

The $unique_arrayyy includes multidimentional array values. I want to run query inside the array_map fuction. At the time of fetching the row its giving me blank window. Please help.

Upvotes: 0

Views: 70

Answers (1)

jeroen
jeroen

Reputation: 91734

You don't have the connection available in the scope of your anonymous function, $conn is undefined.

array_map(function ($var) use ($conn) {
                          ^^^^^^^^^^^ Make $conn available in the function
    $fetch_gdd=mysqli_query($conn,"select * from grade");

    echo mysqli_num_rows($fetch_gdd);
    // don't exit here

}, $unique_arrayyy);

Also note that you probably don't want to stop the script in your function, so I have removed the exit; statement as well.

Also note that I assume that this is just example code; you don't use $var inside your function and if you do the same query every time, you should not do that in the array_map function.

Upvotes: 1

Related Questions