当前位置: 首页 > 图文教程 > 网络编程 > PHP > MySQL通用查询程序

PHP
php3:跨平台的服务器端嵌入式脚本语言
关于Zend Optimizer
用PHP3实现文件上载
IIS环境下安装PHP5手记
在Windows2000ADV下配置Apache+PHP5+MySql5
Winodws下IIS/Apache+PHP+MySQL的安装配置
PHP 在Windows 2003 Enterprise Server 、IIS6.0 下的安装
在WIN平台上让你的 Apache 2.0.45 支持 PHP
从实例开始
能得到你是从什么页面过来的,referer的用处
在PHP5中使用DOM控制XML
旧题新貌:PHP截取中文字符串的问题
GraPHPite--PHP图像库新秀
编译php的配置参数
PHP程序与服务器端通讯方法小结
PHP程序与服务器端通讯的方法
用php或js获取图片大小,高宽尺寸.
有关在Windows下配置PHP+Apache+Optimizer失败的问题解决方案
LAMPJT最适用的web开发系统详细配置
实现一个基于Ajax的调查程序

PHP 中的 MySQL通用查询程序


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 56 ::
收藏到网摘: n/a

  if(get_magic_quotes_gpc()==1){
   ?>
<html>
<head><title>MySQL通用查询程序</title></head>
<body>
注意本程序需要将PHP配置文件(PHP3为php3.ini,PHP4为php.ini)中的magic_quotes_gpc
设成Off或0,修改后请重新启动Apache.
</body>
</html>
   <?
   exit();
}

set_magic_quotes_runtime(0);

$host = 'localhost';
$db = 'test';
$user = 'test';
$pass = '';

// [ php/inc/str2url.php ] cvs 1.2
function str2url($path){
   return eregi_replace("%2f","/",urlencode($path));
}
?>

<html>
<head><title>MySQL通用查询程序</title></head>
<body>

<form action="<?echo str2url($PHP_SELF);?>" method="post">
请输入SQL语句:<br>
<textarea name="sql" cols="100" rows="5"><?echo $sql;?></textarea><br>
<input type="submit" name="cmd" value="查询">
<input type="submit" name="cmd" value="执行">
</form>

<?
if($cmd){
   $con = mysql_pconnect($host,$user,$pass) or die('无法连接'.$host.'服务器');
   mysql_select_db($db,$con) or die('无法连接'.$db.'数据库');
   $rst = mysql_query($sql,$con) or die($sql.'出错');
   if($cmd=='查询'){
      $num_fields = mysql_num_fields($rst);
      echo '<hr>';
      echo '<table border="1" cellpadding="0" cellspacing="0">';
      echo '<caption align="center">'.$sql.'</option>';
      echo '<tr>';
      for($i=0;$i<$num_fields;$i++) echo '<th>&nbsp;'.mysql_field_name($rst,$i).'</th>';
      echo '</tr>';
      while($row=mysql_fetch_row($rst)){
         echo '<tr>';
         for($i=0;$i<$num_fields;$i++) echo '<td>&nbsp;'.$row[$i].'</td>';
         echo '</tr>';
      }
      echo '</table>';
      mysql_free_result($rst);
   }
   else echo '有 '.mysql_affected_rows($con).' 行受影响';
}
?>

</body>
</html>