Home>

Let me introduce you today.How to automate using php.sql file. it's actually really easy,Is to get the contents of the sql file,Then execute each SQL statement once.

//Read file contents
$_sql=file_get_contents ("test.sql");
$_arr=explode (";", $_sql);
$_mysqli=new mysqli (db_host, db_user, db_pass);
if (mysqli_connect_errno ()) {
  exit ("Error connecting to the database");
}
//Execute the SQL statement
foreach ($_arr as $_value) {
  $_mysqli->query ($_ value. ";");
}
$_mysqli->close ();
$_mysqli=null;
</pre>
</div>
<p>
The text.sql above is the sql file you need to execute, db_host hostname, db_user username, db_pass password!
</p>
<p>
This is just the most basic automatic SQL file. You can also customize the name of the generated database.
The method is to delete the following code in the sql file
</p>
<div>
<pre>
create database if not exists database name default character set utf8 collate utf8_general_ci;

use database name

Then add code before executing all sql statements in text.php

$_mysqli->query ("create database if not exists database name default character set utf8 collate utf8_general_ci;");
$_mysqli->query ("use database name");
php
  • Previous C # Chinese character to Pinyin example (supports all Chinese characters in the GB2312 character set)
  • Next Ajax debugging steps under jQuery