『壹』 如何利用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中使用SQL語句可以通過倒序排列記錄取出第一條的記錄取到最後一條數據。
一般,php調用mysql的介面查詢,查詢語句如下:
select * from table order by id DESC limit 1
這樣就取出記錄的最後一條記錄。
『叄』 php從mysql表中查詢count(學號)然後賦值給一個變數,,並輸出
首先你返回的結果是一個資源型數據 需要處理
其次你count()的欄位使用主鍵把 也就是 count(主鍵欄位) 這樣就沒問題了