phpsql写入数据 php数据库写入-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

phpsql写入数据 php数据库写入

如何利用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_dump($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_dump($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_ ); ? lishixinzhi/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中使用sql语句写入数据,怎样在$_POST['ID']插入一个变量

改成{$_POST['Region'.$jc]},

原先的{$_POST['Region".$jc."']},{}里的“”是相对独立的,不合外面的“”配对,也就是说“”就是单引号包含的一串字符串中的字符,所以,显而易见的PHP不会对".$jc."做concat处理,只是作为一串字符。


分享题目:phpsql写入数据 php数据库写入
本文来源:http://kswsj.cn/article/dddjsji.html

其他资讯