您的位置:

php如何取sql,php取地址符

本文目录一览:

php怎么获取上一条执行的sql语句

原生代码不支持获取上一次执行的

sql语句

,ThinkPHP用getLastSql()函数获取的,代码如下:

class UserModel extends Model(BaseModel){

public function test($data){

$model = M('user');

$model-add($data);

$sql = $model-getLastSql();

} TP还有一种种方法就是直接用_sql,这个更方便!

1.

$model-_sql();方法实际执行的就是

2.

$model-getLastSql();用_sql注意中文

乱码

问题,相来说还是getlastsql比较快,_有时候会因为中文编码出错

至于大小写的问题根本不存在

php不区分这些大小写的。

PHP如何取得mysql字段值并记算时间?

两种方式 :

1、可以直接用SQL 里的函数 sum() ;

如:"select sum(字段) as num from tab "; //sum 取该字段所有列的总和

2、用PHP函数 sum($array) ; //取数组中值的总和

当然 字段必须是int型的

如何利用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