导航:首页 > 编程语言 > phpsqlcount

phpsqlcount

发布时间:2023-08-26 11:50:56

‘壹’ 如何利用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(主键字段) 这样就没问题了

阅读全文

与phpsqlcount相关的资料

热点内容
订购命令英语 浏览:653
java正则网址 浏览:777
程序员上班可不可以自学 浏览:428
空调压缩机排空气视频 浏览:283
centos72nginxphp 浏览:184
游戏平台用什么服务器好 浏览:753
保密柜里的图片是加密文件吗 浏览:909
php判断最后一个字符 浏览:635
pdf脑区 浏览:635
at命令已弃用 浏览:490
买点卖出指标源码 浏览:612
36位单片机 浏览:428
英雄联盟山东服务器地址 浏览:212
sd服务器什么意思 浏览:617
thinkphp去indexphp 浏览:62
电脑显示连接未加密 浏览:193
zao服务器怎么修改 浏览:244
php使用jsapi调起支付 浏览:891
vivo云服务器网 浏览:722
cmd远程连接命令行 浏览:961