Last active
January 3, 2016 05:50
-
-
Save saleemkce/8418921 to your computer and use it in GitHub Desktop.
Execute multiple SQL statements in PHP for Mysql database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Execute multiple SQL statements in PHP or directly execute .SQL files in MySql using PHP | |
*/ | |
ini_set('display_errors', 0); | |
error_reporting(0); | |
// SET MYSQL CONFIGURATION | |
$serverName = 'localhost'; | |
$username = 'root'; | |
$password = ''; | |
$database = 'test_delete'; | |
// SET THE SQL FILE PATH OR DIRECTLY GIVE ALL SQL STATEMENTS INSIDE QUOTES | |
$query = file_get_contents('file.sql'); | |
// Establishing connection with mysqli database | |
$con = new mysqli($serverName, $username, $password, $database); | |
/* check connection */ | |
if(mysqli_connect_errno()) { | |
printf("Connect failed: %s\n", mysqli_connect_error()); | |
exit(); | |
} | |
/* execute multi query */ | |
if($con->multi_query($query)) | |
{ | |
do { | |
/* store first result set */ | |
if($resultSet = $con->store_result()) | |
{ | |
while($row = $resultSet->fetch_row()) | |
{ | |
printf("%s\n", $row[0]); | |
} | |
$resultSet->free(); | |
} | |
//print divider | |
if($con->more_results()) | |
{ | |
$loadArray = array("Creating tables....", "please wait..", "stay tuned while all table definitions are dumped..."); | |
$upperLimit = count($loadArray) - 1; | |
$randNumb = rand(0, $upperLimit); | |
echo $loadArray[$randNumb]; echo '<br/>'; | |
$loadArray = array(); | |
} | |
} while ($con->next_result()); | |
echo '<br/><br/><span style="font-weight: bold;">All tables have been successfully copied/created to given database!</span>'; | |
/* close connection */ | |
} | |
$con->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment