Jump to content

problem with file_get_Contents()


phpmady

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.