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
https://forums.phpfreaks.com/topic/199969-problem-with-file_get_contents/
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

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

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

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

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,

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.