pdo mysql 执行多条sql_php实现的简单mysql工具(执行多条sql语句)

代码如下:

header('Content-Type:text/html;charset=GBK');

?>

/**

* 简单mysql工具

* 执行多条sql语句

* edit by www.jbxue.com

*/

if(isset($_GET['sg'])){

$_SESSION['sg']=$_GET['s'];

}

if(isset($_GET['su'])){

$_SESSION['su']=$_GET['s'];

}

if(isset($_POST['sql'])){

$conn = mysql_connect('127.0.0.1', "root", "123qwe");

if (!$conn) {

echo "Unable to connect to DB: " . mysql_error();

exit;

}

if (!mysql_select_db("pm")) {

echo "Unable to select mydbname: " . mysql_error();

exit;

}

if($_SESSION['sg']){

mysql_query("set names 'gbk'") or die(mysql_error());

}

if($_SESSION['su']){

mysql_query("set names 'utf8'") or die(mysql_error());

}

$sql =trim($_POST['sql']);

preg_match_all("/(select|show|update|delete|drop|create|alter|insert)/s+(([`'/"])[^`'/"]+//3|[^;])+;?/i",$sql,$out,PREG_PATTERN_ORDER );

if(count($out[0])==0) echo "No sql
";

//解决不输入无分号找不到sql的问题

for($i=0;$i

$sql = $out[0][$i];

if(substr(strtolower($sql),0,6)=='select'&&strpos($sql,"()")===false&&!preg_match("/limit /d+(,/d+)?$/i",$sql)){

$sql .= " limit 100";

}

echo '$sql='.$sql.'


';

$result = mysql_query(stripslashes($sql));

if (!$result) {

echo "Could not successfully run query ($sql) from DB: " . mysql_error()."";

continue;

}

if (mysql_num_rows($result) == 0) {

echo "No rows found, nothing to print so am exiting";

continue;

}

// While a row of data exists, put that row in $row as an associative array

// Note: If you're expecting just one row, no need to use a loop

// Note: If you put extract($row); inside the following loop, you'll

// then create $userid, $fullname, and $userstatus

$str = "";

while ($row = mysql_fetch_assoc($result)) {

if($str == ""){

$str = '

';

foreach($row as $k=>$v){

$str .= "

".$k."";

}

$str .= "

";

}

$str .= "

";

foreach($row as $k=>$v){

$str .= "

".$v."";

}

$str .= "

";

}

@mysql_free_result($result);

echo "

echo $str;

echo "

";

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值