0

I have a php script that is supposed to execute several mysql statements, everything works if there are no comments /* */ and no breaklines...

Could you please help me add this functionality, also ignore -- comments

<?
$sqlFileToExecute = 'mysql_dump.sql';
$hostname          = 'localhost';
$db_user           = 'root';
$db_password      = '';
$database_name    = 'db_';
$link = mysql_connect($hostname, $db_user, $db_password);
if (!$link) {
  die ("error connecting MySQL");
}

mysql_select_db($database_name, $link) or die ("wrong DB");
$f = fopen($sqlFileToExecute,"r+");
$sqlFile = fread($f, filesize($sqlFileToExecute));

$sqlArray = explode(';',$sqlFile);
foreach ($sqlArray as $stmt) {

 //THIS SEEMS NOT TO WORK 
 if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') {
    $result = mysql_query($stmt);
    if (!$result) {
      $sqlErrorCode = mysql_errno();
      $sqlErrorText = mysql_error();
      $sqlStmt = $stmt;
      break;
    }
  }
}
if ($sqlErrorCode == 0) {
  echo "SETUP COMPLETED ;)";
} else {
  echo "FAIL!<br/>";
  echo "Error code: $sqlErrorCode<br/>";
  echo "Error text: $sqlErrorText<br/>";
  echo "Statement:<br/> $sqlStmt<br/>";
}

?>
5
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Mar 18, 2013 at 16:18
  • what errors are you getting? Commented Mar 18, 2013 at 16:18
  • Before finish read this question, I strongly suggest to dismiss mysql_* calls, because are deprecated. Use PDO instend or mysqli Commented Mar 18, 2013 at 16:18
  • looks like your processing an SQL dump file. in that case just load it all together stackoverflow.com/questions/2712910/… Commented Mar 18, 2013 at 16:20
  • You need to replace stuff like $sqlArray = explode(';',$sqlFile); with a full fledged SQL parser. It's by no means a trivial task—I suggest you just find a third-party one. (Whatever, comments are ignored automatically by MySQL!). Commented Mar 18, 2013 at 16:40

1 Answer 1

1

Are you sure you need to run it this manner:

You can also use

$mysql -pxxx -u username db_name -vvv < sourcefile.sql >/tmp/outfile.log

Enable verbose mode to check the stats, else you can omit -vvv option

OR

mysql > source "sourcefile.sql"

You can see more options here

Sign up to request clarification or add additional context in comments.

2 Comments

suppose I can not have direct access to server mysql console, so fastest way to do it is executing php script
what I am doing is uploading to the server (only ftp access) this script and the file .sql that contains the sql statements. Then as I know the user and database available (I want to create a table or something) I execute the php script and then I am able to use those created tables, now the problem yields in linebraks and comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.