phpmady Posted April 27, 2010 Share Posted April 27, 2010 Hi, I am trying to load the sql file but am nothing getting in db, whats am doing wrong? mysql_connect($hostname,$username,$password) or die(mysql_error()); //create the database here and pass down the statement $query = "CREATE DATABASE $dbname"; $result = mysql_query($query); mysql_select_db($dbname) or die(mysql_error()); $queryFile = 'cmstouch2.sql'; $sql1 = file_get_contents($queryFile); $sql = trim($sql1); $result = mysql_query($sql); if($result) { echo "sucess"; } else { echo "failed"; } Thank You Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2010 Share Posted April 27, 2010 You cannot execute .sql backup/dump files through a msyql_query() statement because they are made up of multiple lines of sql code and comments that you must parse through and only execute the sql commands, one at a time. Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 27, 2010 Author Share Posted April 27, 2010 You cannot execute .sql backup/dump files through a msyql_query() statement because they are made up of multiple lines of sql code and comments that you must parse through and only execute the sql commands, one at a time. ok i couldnt get an idea, will u please give me more input Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 28, 2010 Author Share Posted April 28, 2010 here is the code i tried out, whats wrong <?php if(isset($_POST['install'])) { $hostname = $_POST['hostname']; $username = $_POST['username']; $password = $_POST['password']; $dbname = "touch_".$_POST['dbname']; $dbinstall = $_POST['dbinstall']; //if everything ok above procedd to load the sql file in mysql //and returns the dbname // dont forget to update the data in the tables for hostname, username, password, //echo $dbinstall; if($dbinstall!="") { $dbhandles = mysql_connect($hostname,$username,$password) or die(mysql_error()); //create the database here and pass down the statement $query = "CREATE DATABASE $dbname"; $result = mysql_query($query); //$dbhandle = mysql_select_db($dbname,$dbhandles); mysql_select_db($dbname) or die(mysql_error()); $sqlFileToExecute = 'cmstouch2.sql'; $f = fopen($sqlFileToExecute,"r+"); $sqlFile = fread($f,filesize($sqlFileToExecute)); $sqlArray = explode(';',$sqlFile); //Process the sql file by statements foreach ($sqlArray as $stmt) { if (strlen($stmt)>3){ echo $stmt; $result = mysql_query($stmt); echo $result; if (!$result){ $sqlErrorCode = mysql_errno(); $sqlErrorText = mysql_error(); $sqlStmt = $stmt; break; } } } } echo "Halas"; } ?> thank you Quote Link to comment Share on other sites More sharing options...
anups Posted April 28, 2010 Share Posted April 28, 2010 Exploding on [;] will not work because there may be [;] in internal insert values. 1. You cannot run multiple queries at once using the mysql_* functions. So you have to create the query, run it through mysql_query(), and repeat. 2. With the mysqli_* functions, you can run multiple queries at once. USE mysqli_multi_query Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 28, 2010 Author Share Posted April 28, 2010 Exploding on [;] will not work because there may be [;] in internal insert values. 1. You cannot run multiple queries at once using the mysql_* functions. So you have to create the query, run it through mysql_query(), and repeat. 2. With the mysqli_* functions, you can run multiple queries at once. USE mysqli_multi_query Hi Anups, I tried this as you say but only one table is executing.. if($dbinstall!="") { $dbhandles = mysql_connect($hostname,$username,$password) or die(mysql_error()); //create the database here and pass down the statement $query = "CREATE DATABASE $dbname"; $result = mysql_query($query); //$dbhandle = mysql_select_db($dbname,$dbhandles); mysql_select_db($dbname) or die(mysql_error()); $link = mysqli_connect($hostname,$username,$password,$dbname); $sqlFileToExecute = 'cmstouch2.sql'; $f = fopen($sqlFileToExecute,"r+"); $sqlFile = fread($f,filesize($sqlFileToExecute)); $sqlArray = explode(';',$sqlFile); //Process the sql file by statements foreach ($sqlArray as $stmt) { echo "hi"; //echo $stmt; $result = mysqli_multi_query($link,$stmt); echo $result; $stmt = ""; } } Thank you Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 28, 2010 Author Share Posted April 28, 2010 You cannot execute .sql backup/dump files through a msyql_query() statement because they are made up of multiple lines of sql code and comments that you must parse through and only execute the sql commands, one at a time. Hi, I am very confused, regarding the loading the sql file and executing it via PHP. Suggest me how to carry out. You said mysql_query() cant be used in this operation, if mysql_query() didnt whats the alternative... thank you, Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 28, 2010 Author Share Posted April 28, 2010 Hi guys, Do anyone had an idea about this? Thank you Quote Link to comment Share on other sites More sharing options...
anups Posted April 28, 2010 Share Posted April 28, 2010 TRY following... I just tried $dbName = "testdb"; $dbhandles = mysqli_connect("localhost","root","") or die(mysql_error()); $query = "CREATE DATABASE $dbName"; mysqli_query($dbhandles,$query); mysqli_select_db($dbhandles,$dbName) or die(mysqli_error($dbhandles)); $sqlFileToExecute = 'sql.sql'; $Queries = file_get_contents($sqlFileToExecute); mysqli_multi_query($dbhandles,$Queries); Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 28, 2010 Author Share Posted April 28, 2010 TRY following... I just tried $dbName = "testdb"; $dbhandles = mysqli_connect("localhost","root","") or die(mysql_error()); $query = "CREATE DATABASE $dbName"; mysqli_query($dbhandles,$query); mysqli_select_db($dbhandles,$dbName) or die(mysqli_error($dbhandles)); $sqlFileToExecute = 'sql.sql'; $Queries = file_get_contents($sqlFileToExecute); mysqli_multi_query($dbhandles,$Queries); Hi Singh, Thanks for ur support, but the above coulnt work... thank You Quote Link to comment Share on other sites More sharing options...
phpmady Posted April 28, 2010 Author Share Posted April 28, 2010 I got the solution you guys face the problem like me, u can browse here for solution http://www.ozerov.de/bigdump.php Thanks a lot for all Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.