I have a function that gets user details and returns an associative array consisting of arrays of each user with its related data. My function works except that it doesn't work as well when it has to fetch large number of rows from mySQL.
function function_name($DB, $id)
{
//Prepare, bind and execute statements
//Returns one value or an array
}
function main_function($DB, $id_list)
{
foreach($id_list as $user_id)
{
//Calls function_name
$data = function_name($DB, $user_id);
}
//Returns a nested associative array
}
I have been told that I should move the bind param statement outside of the foreach loop in my case but I have tried and I keep getting error "MySQL has went away" message. How can I optimise querying from mysql when I could potentially be querying 10,0000 id at one time?
Please refer to the code snippet below for detailed explanation.
function getUserEvent($DB_3308, $user_id)
{
$user_event = array ();
$sql_get_user_event = "SELECT * FROM user_event WHERE user_id = ?";
$statement_user_event = $DB_PUMA_3306->link->prepare ( $sql_get_user_event);
$statement_user_event ->bind_param ( "s", $user_id );
$statement_user_event ->execute ();
if ($rs_user_event = $statement_user_event->get_result ())
{
while ( $row = $rs_user_event->fetch_assoc () )
{
$user_event [] = $row;
}
}
return $user_event;
}
function getUserDetails($DB_3306, $DB_3308, $user_list)
{
$user_details = array ();
foreach ( $user_list as $user_id )
{
$temp = array ();
$user_personal = null;
$user_event = null;
$user_personal = getUserContact ( $DB_3306, $user_id );
$user_event = getUserEvent( $DB_3308, $userid );
$temp ['user_id'] = $user_id;
$temp ['full_name'] = $user_personal ['full_name'];
$temp ['tel_no'] = $user_personal ['tel_no'];
$temp ['email'] = $user_personal ['email'];
$temp ['events'] = $user_event ;
$user_details [] = $temp;
}
return $user_details;
}