其實(shí)sql文件,就是一些sql語句

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了宣恩免費(fèi)建站歡迎大家使用!
填寫好數(shù)據(jù)庫(kù)相關(guān)操作后,點(diǎn)擊下一步,首先將數(shù)據(jù)庫(kù)連接起來
12mysql_connect(..............)//等等這些數(shù)據(jù)庫(kù)連接代碼
數(shù)據(jù)庫(kù)連接后,開始讀取sql文件
1234567$Sqls = file_get_contents( '你的sql文件' );//然后把讀取到的sql文件內(nèi)容打散成數(shù)組,當(dāng)然,這個(gè)文件要有規(guī)律,就是每條sql語句有一個(gè)特定的分隔符,比如分號(hào);$SqlArr = explode(';', $Sqls );//最后就是循環(huán)遍歷出這些sql語句并執(zhí)行,即可foreach ( $SqlArr as $sql ) { mysql_query( $Sql );}
上面只是一個(gè)大致思路原理,
具體的話,還是要根據(jù)具體情況來弄的!
特別是那個(gè)sql文件中的內(nèi)容,一定要有一定的規(guī)律,并且一些不必要的東西不能有,
比如注釋(很多人從phpmyadmin導(dǎo)出的sql文件,都會(huì)帶上注釋,
而注釋是不符合sql語句規(guī)范的,會(huì)執(zhí)行出錯(cuò),
所以導(dǎo)出后,自己根據(jù)情況修改一下!)
可以參考:
一般是單獨(dú)導(dǎo)入的,
在mysql上,要用mysql_import工具 把文本導(dǎo)入
sqlserver上可以用數(shù)據(jù)庫(kù)備份工具恢復(fù)導(dǎo)入, 也可以使用其他數(shù)據(jù)庫(kù)引擎通過ado到。
不需要源碼,但是需要了解php源碼所需要的庫(kù)表結(jié)構(gòu)。一般php源碼里好多都有建立空庫(kù)結(jié)構(gòu)的源碼。
需要PHP基礎(chǔ)知識(shí)和數(shù)據(jù)庫(kù)基礎(chǔ)知識(shí)。
以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫(kù)。
mysql_connect() 函數(shù)打開MySQL 連接。舉例
?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// 一些代碼...mysql_close($con);
?
mysql_connect()三個(gè)參數(shù)分別是服務(wù)器名,連接賬號(hào),連接密碼。
連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫(kù),后面則是用數(shù)據(jù)庫(kù)語句處理數(shù)據(jù)。SQL語法簡(jiǎn)介網(wǎng)頁鏈接
serial_number.txt的示例內(nèi)容:
serial_number.txt:
DM00001A11 0116,
SN00002A11 0116,
AB00003A11 0116,
PV00004A11 0116,
OC00005A11 0116,
IX00006A11 0116,
創(chuàng)建數(shù)據(jù)表:
create table serial_number(
id int primary key auto_increment not null,
serial_number varchar(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
php代碼如下:
$conn = mysql_connect('127.0.0.1','root','') or die("Invalid query: " . mysql_error());
mysql_select_db('test', $conn) or die("Invalid query: " . mysql_error());
$content = file_get_contents("serial_number.txt");
$contents= explode(",",$content);//explode()函數(shù)以","為標(biāo)識(shí)符進(jìn)行拆分
foreach ($contents as $k = $v)//遍歷循環(huán)
{
$id = $k;
$serial_number = $v;
mysql_query("insert into serial_number (`id`,`serial_number`)
VALUES('$id','$serial_number')");
}
備注:方法有很多種,我這里是在拆分txt文件為數(shù)組后,然后遍歷循環(huán)得到的數(shù)組,每循環(huán)一次,往數(shù)據(jù)庫(kù)中插入一次。
再給大家分享一個(gè)支持大文件導(dǎo)入的
?php
/**
* $splitChar 字段分隔符
* $file 數(shù)據(jù)文件文件名
* $table 數(shù)據(jù)庫(kù)表名
* $conn 數(shù)據(jù)庫(kù)連接
* $fields 數(shù)據(jù)對(duì)應(yīng)的列名
* $insertType 插入操作類型,包括INSERT,REPLACE
*/
function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType='INSERT'){
if(empty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('";
else $head = "{$insertType} INTO `{$table}`(`".implode('`,`',$fields)."`) VALUES('"; //數(shù)據(jù)頭
$end = "')";
$sqldata = trim(file_get_contents($file));
if(preg_replace('/\s*/i','',$splitChar) == '') {
$splitChar = '/(\w+)(\s+)/i';
$replace = "$1','";
$specialFunc = 'preg_replace';
}else {
$splitChar = $splitChar;
$replace = "','";
$specialFunc = 'str_replace';
}
//處理數(shù)據(jù)體,二者順序不可換,否則空格或Tab分隔符時(shí)出錯(cuò)
$sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替換換行
$sqldata = $specialFunc($splitChar,$replace,$sqldata); //替換分隔符
$query = $head.$sqldata.$end; //數(shù)據(jù)拼接
if(mysql_query($query,$conn)) return array(true);
else {
return array(false,mysql_error($conn),mysql_errno($conn));
}
}
//調(diào)用示例1
require 'db.php';
$splitChar = '|'; //豎線
$file = 'sqldata1.txt';
$fields = array('id','parentid','name');
$table = 'cengji';
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
/*sqlda ta1.txt
1|0|A
2|1|B
3|1|C
4|2|D
-- cengji
CREATE TABLE `cengji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
*/
//調(diào)用示例2
require 'db.php';
$splitChar = ' '; //空格
$file = 'sqldata2.txt';
$fields = array('id','make','model','year');
$table = 'cars';
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
/* sqldata2.txt
11 Aston DB19 2009
12 Aston DB29 2009
13 Aston DB39 2009
-- cars
CREATE TABLE `cars` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`make` varchar(16) NOT NULL,
`model` varchar(16) DEFAULT NULL,
`year` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
*/
//調(diào)用示例3
require 'db.php';
$splitChar = ' '; //Tab
$file = 'sqldata3.txt';
$fields = array('id','make','model','year');
$table = 'cars';
$insertType = 'REPLACE';
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
/* sqldata3.txt
11 Aston DB19 2009
12 Aston DB29 2009
13 Aston DB39 2009
*/
//調(diào)用示例3
require 'db.php';
$splitChar = ' '; //Tab
$file = 'sqldata3.txt';
$fields = array('id','value');
$table = 'notExist'; //不存在表
$result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
if (array_shift($result)){
echo 'Success!br/';
}else {
echo 'Failed!--Error:'.array_shift($result).'br/';
}
//附:db.php
/* //注釋這一行可全部釋放
?
?php
static $connect = null;
static $table = 'jilian';
if(!isset($connect)) {
$connect = mysql_connect("localhost","root","");
if(!$connect) {
$connect = mysql_connect("localhost","Zjmainstay","");
}
if(!$connect) {
die('Can not connect to database.Fatal error handle by /test/db.php');
}
mysql_select_db("test",$connect);
mysql_query("SET NAMES utf8",$connect);
$conn = $connect;
$db = $connect;
}
?
//*/
.
-- 數(shù)據(jù)表結(jié)構(gòu):
-- 100000_insert,1000000_insert
CREATE TABLE `100000_insert` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
100000 (10萬)行插入:Insert 100000_line_data use 2.5534288883209 seconds
1000000(100萬)行插入:Insert 1000000_line_data use 19.677318811417 seconds
//可能報(bào)錯(cuò):MySQL server has gone away
//解決:修改my.ini/my.cnf max_allowed_packet=20M
如果你使用thinkphp框架,可以有直接的語法提交數(shù)組到數(shù)據(jù)庫(kù)。
或者可以考慮以下我編寫的示例代碼:
?php
header('Content-type:text/html;charset=utf-8');
$values = array('isbn'='0-672-31509-8','author'='王一','title'='php高級(jí)編程','price'=58.00);
$sql = sprintf('INSERT INTO %s (%s) VALUES ("%s")', 'books',
implode(', ', array_map('mysql_escape_string', array_keys($values))),
implode('", "',array_map('mysql_escape_string', $values)));
$db = new mysqli('localhost','root','root','books');
$db-query($sql);
?
一、php配置MySQL
1、將php安裝目錄下的php_mysql.dll和MySQL安裝目錄下的libmysql.dll文件拷貝至c:/windows/system32中;
2、配置php.ini
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_mysql.dll
extension=php_mysqli.dll
把上面四個(gè)。dll的最前面的;去掉
二、php表單提交至數(shù)據(jù)庫(kù)的實(shí)現(xiàn)過程
1、login.php頁面
SPAN style="FONT-SIZE: 14px"html
FORM method=post action=add.php
Name: INPUT name=usernameBR
Email: INPUT name=emailBR
INPUT value=提交 type=submit name=submit
/FORM
/SPAN
2、add.php頁面
SPAN style="FONT-SIZE: 14px"?php
include("conn.php");
?
?php
if(isset($_POST["submit"]))
{
$sql = "insert into users(username, email) values('$_POST[username]', '$_POST[email]')";
mysqli_query($conn, $sql);
echo "添加成功";
}
?/SPAN
3、conn.php頁面
SPAN style="FONT-SIZE: 14px"?php
$conn = new mysqli("localhost", "root", "159357");
$conn-select_db("db_test");
//mysql_query("set name 'gb2312'");
$conn-set_charset("utf8");
?/SPAN
網(wǎng)頁題目:php實(shí)現(xiàn)數(shù)據(jù)傳到數(shù)據(jù)庫(kù) php讀取數(shù)據(jù)庫(kù)內(nèi)容并輸出
轉(zhuǎn)載來源:http://chinadenli.net/article42/dooijhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、、微信公眾號(hào)、動(dòng)態(tài)網(wǎng)站、做網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)