1. 云栖社区>
  2. PHP教程>
  3. 正文

PHP Script to Execute MySQL statements in a text file

作者:用户 来源:互联网 时间:2017-12-01 14:21:43

PHP Script to Execute MySQL statements in a text file - 摘要: 本文讲的是PHP Script to Execute MySQL statements in a text file, If you have a few routineSQL statements to execute, you can save them in a *.sql file and login to MySQL console to is

If you have a few routineSQL statements to execute, you can save them in a *.sql file and login to MySQL console to issue command source . Alternative, you can run these commands using PHP function.

Save the following database credentials in a separate PHP file.

PHP Script to Execute MySQL statements in a text file

MySQL

// conn.phpdefine('DB_HOST', 'helloacm.com');define('DB_USER', 'DB_USER';define('DB_PASSWORD', 'DB_PASSWORD');define('DB_NAME', 'DB_NAME'); // conn.phpdefine('DB_HOST', 'helloacm.com');define('DB_USER', 'DB_USER';define('DB_PASSWORD', 'DB_PASSWORD');define('DB_NAME', 'DB_NAME');

Then this does the job by reading the SQL script file and execute them each one by one, and line by line only. You would then need to invoke the function ExecSqlFile($filename .

require('conn.php');mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);mysql_query("SET NAMES utf8");mysql_select_db(DB_NAME);function ExecSqlFile($filename) { if (!file_exists($filename)) { return false; } $querys = explode("/n", file_get_contents($filename)); foreach ($querys as $q) { $q = trim($q); if (strlen($q)) { mysql_query($q) or die(mysql_error()); } } return true;} require('conn.php');mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);mysql_query("SET NAMES utf8");mysql_select_db(DB_NAME);function ExecSqlFile($filename) { if (!file_exists($filename)) { return false; } $querys = explode("/n", file_get_contents($filename)); foreach ($querys as $q) { $q = trim($q); if (strlen($q)) { mysql_query($q) or die(mysql_error()); } } return true;}

You can save these two files and put it oncrontab job, so that it can be executed on a scheduled basis.

–EOF–

GD Star Rating

loading...

GD Star Rating

loading...

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。