0

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.

1
  • Add error_reporting(E_ALL); ini_set('display_errors',1); on top of page just after <?php ad also change like:- $fetch_gdd=mysqli_query($conn,"select * from grade")or die (mysqli_error($conn)); Commented Oct 1, 2016 at 8:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.