① 如何利用php执行.SQL文件
本篇文章是对使用PHP执行 SQL文件的实现代码进行了详细的分析介绍 需要的朋友参考下demo php:
复制代码 代码如下: <?php /** * 读取 sql 文件并写入数据库 * @version demo php */ class DBManager { var $dbHost = ; var $dbUser = ; var $dbPassword = ; var $dbSchema = ; function __construct($host $user $password $schema) { $this >dbHost = $host; $this >dbUser = $user; $this >dbPassword = $password; $this >dbSchema = $schema; } function createFromFile($sqlPath $delimiter = (;/n)|((;/r/n))|(;/r) $prefix = $menter = array( # )) { //判断文件是否存在 if(!file_exists($sqlPath)) return false; $handle = fopen($sqlPath rb ); $sqlStr = fread($handle filesize($sqlPath)); //通过sql语法的语句分割符进行分割 $segment = explode(";" trim($sqlStr)); //var_mp($segment); //去掉注释和多余的空行 foreach($segment as & $statement) { $sentence = explode("/n" $statement); $newStatement = array(); foreach($sentence as $subSentence) { if( != trim($subSentence)) { //判断是会否是注释 $isComment = false; foreach($menter as $er) { if(eregi("^(" $er ")" trim($subSentence))) { $isComment = true; break; } } //如果不是注释 则认为是sql语句 if(!$isComment) $newStatement[] = $subSentence; } } $statement = $newStatement; } //对表名加前缀 if( != $prefix) { //只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName $regxTable = "^[/`/ /"]{ }[/_a zA Z]+[/_a zA Z ]*[/`/ /"]{ }$";//处理表名的正则表达式 $regxLeftWall = "^[/`/ /"]{ }"; $sqlFlagTree = array( "CREATE" => array( "TABLE" => array( "$regxTable" => ) ) "INSERT" => array( "INTO" => array( "$regxTable" => ) ) ); foreach($segment as & $statement) { $tokens = split(" " $statement[ ]); $tableName = array(); $this >findTableName($sqlFlagTree $tokens $tableName); if(empty($tableName[ leftWall ])) { $newTableName = $prefix $tableName[ name ]; } else{ $newTableName = $tableName[ leftWall ] $prefix substr($tableName[ name ] ); } $statement[ ] = str_replace($tableName[ name ] $newTableName $statement[ ]); } } //组合sql语句 foreach($segment as & $statement) { $newStmt = ; foreach($statement as $sentence) { $newStmt = $newStmt trim($sentence) "/n"; } $statement = $newStmt; } //用于测试 //var_mp($segment); //writeArrayToFile( data txt $segment); // self::saveByQuery($segment); return true; } private function saveByQuery($sqlArray) { $conn = mysql_connect($this >dbHost $this >dbUser $this >dbPassword); mysql_select_db($this >dbSchema); foreach($sqlArray as $sql) { mysql_query($sql); } mysql_close($conn); } private function findTableName($sqlFlagTree $tokens $tokensKey= & $tableName = array()) { $regxLeftWall = "^[/`/ /"]{ }"; if(count($tokens)<=$tokensKey) return false; if( == trim($tokens[$tokensKey])) { return self::findTableName($sqlFlagTree $tokens $tokensKey+ $tableName); } else { foreach($sqlFlagTree as $flag => $v) { if(eregi($flag $tokens[$tokensKey])) { if( ==$v) { $tableName[ name ] = $tokens[$tokensKey]; if(eregi($regxLeftWall $tableName[ name ])) { $tableName[ leftWall ] = $tableName[ name ]{ }; } return true; } else{ return self::findTableName($v $tokens $tokensKey+ & $tableName); } } } } return false; } } function writeArrayToFile($fileName $dataArray $delimiter="/r/n") { $handle=fopen($fileName "wb"); $text = ; foreach($dataArray as $data) { $text = $text $data $delimiter; } fwrite($handle $text); } //测试 $dbM = new DBManager( localhost w f test ); $dbM >createFromFile( data sql null fff_ ); ?> lishixin/Article/program/PHP/201311/21281
② php 如何把一条sql语句写入数据库
<?php
$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('连接失败: ' . mysqli_error($conn))
;}
echo '连接成功<br />'; // 设置编码,防止中文乱码mysqli_query($conn , "set names utf8");
$runoob_title = '学习 Python';
$runoob_author = 'RUNOOB.COM';
$submission_date = '2016-03-06';
$sql = "INSERT INTO runoob_tbl ".
"(runoob_title,runoob_author, submission_date) ".
"VALUES ".
"('$runoob_title','$runoob_author','$submission_date')";
mysqli_select_db( $conn, 'RUNOOB' );$retval = mysqli_query( $conn, $sql );
if(! $retval ){
die('无法插入数据: ' . mysqli_error($conn))
;}
echo "数据插入成功\n";
mysqli_close($conn);
?>
按照步骤开始,多看PHP手册。
③ php写入数据库
PHP向MySQL数据库中写入数据有三个步骤:
1,PHP和MySQL建立连接关系
2,打开MySQL数据库
3,接受页面数据,PHP录入到指定的表中
1、2两步可直接使用一个数据库链接文件即可:conn.php
代码如下
<?php
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
?>
当然,前提是已经安装WEB服务器、PHP和MySQL,并且建立MySQL表“cnbruce”
mysql_connect()中三个参数分别为MySQL地址、MySQL用户名和MySQL密码
然后就是通过WEB页面传递数据,让PHP通过SQL语句将数据写入MySQL数据库指定的表中,比如新建文件 post.php
代码如下
<?php
require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
?>
测试页面: http://localhost/post.php?n=cnbruce&p=i0514
即可向MySQL数据库hello的members表中插入新的数据“cnbruce”到username字段、“i0514”到password字段
补充:读取表
读取表中的内容,这里我们用while,可以根据具体情况,用for 或其他的.
代码如下
while($row = mysql_fetch_array($result))
{
echo "<div style="height:24px; line-height:24px; font-weight:bold;">"; //排版代码
echo $row['Topic'] . "<br/>";
echo "</div>"; //排版代码
④ php中插入MySQL数据库的语句怎么写
显示数据库或表:
showdatabases;//然后可以usedatabase_name;
showtables;
更改表名:
altertabletable_namerenamenew_t;
添加列:
altertabletable_nameaddcolumnc_ncolumnattributes;
删除列:
altertabletable_namedropcolumnc_n;
创建索引:
altertablec_tableaddindex(c_n1,c_n2);
altertablec_tableadniqueindex_name(c_n);
altertablec_tableaddprimarykey(sid);
删除索引:
altertablec_tabledropindexc_n1;
更改列信息:
altertablet_tablechangec_1c_1varchar(200);
altertablet_tablemodify1c_1varchar(200);
insert插入语句:
insertintotable_name(c_1,c_2)
values('x1',1);
update语句:
updatetable_namesetc_1=1wherec_2=3;
删除数据库或者表:
droptabletable_name;
dropdatabasedatabase_name;//使用mysql_drop_db()可以删除的.