I have created a CMS. When installing the CMS, I must install its database. I use PHP multi_query()
to istall the database without opening phpMyAdmin. When the SQL file is as small as 624KB, the database is installed successfully. However, when the SQL file is as large as 6.32MB or more, the database does not install. Here is the code I use to install the database via PHP
$sParamSqlFile = 'database.sql';
if(file_exists($sParamSqlFile)){
$fSql = file_get_contents($sParamSqlFile);
/* execute multi query */
if ($oDbConn->multi_query($fSql)){
do {
/* store first result set */
if ($oResult = $oDbConn->store_result()) {
while ($aRow = $oResult->fetch_row()) {
//
}
$oResult->free();
};
/* print divider */
if ($oDbConn->more_results()){
//
}
} while ($oDbConn->next_result());
}
else{
return 'false';
}
/* close connection */
$oDbConn->close();
return 'true';
}
return $sParamSqlFile . ' does not exist';
Edit: I have encounted this error while trying to install the database "Warning: mysqli::multi_query(): Error while reading SET_OPTION’s response packet. PID=8660"
Could you help with a solution so that I could install the database when the SQL file is large?
2
Answers
I referred to this answer https://stackoverflow.com/a/5688506/12963244 to solve the problem.
I ran this SQL statement in phpMyAdmin to show the current
max_allowed_packet
The
max_allowed_packet
was 1048576. I changed it to 16777216 by running the following SQL statement in phpMyAdminAfter changing the
max_allowed_packet
to 16777216, I was able to install my database from a SQL file of about 6.32MB successfully using PHP'smulti_query()
.I did this in Xampp. I am told this solution may not work in shared hosting where you may not be allowed by your web hosting company to change the
max_allowed_packet
this wayYou should be checking what the error is when a query fails.
Most likely the packet size is too large.
Try parsing the data – mysqldump will produce something with a well defined structure that is easy to split into individual statements (you just need to ensure that any occurrence of ‘;’ NOT within single quotes marks the end of statement – then run the statements one at a time.