像下面这个程序:
<? header("Content-type: text/html; charset=gb2312"); $act=$_GET["act"];if($act=="del") { //删除记录 $id =$_GET["id"]; require('conn.php'); $conn->query("delete from lyb where id=$id"); fy(); }if($act=="list") { fy();}function fy() {require('conn.php'); $sql="select * from lyb order by ID desc"; //echo $sql;$result=$conn->query($sql);}
<? header("Content-type: text/html; charset=gb2312"); require('conn.php'); $act=$_GET["act"];if($act=="del") { //删除记录 $id =$_GET["id"]; $conn->query("delete from lyb where id=$id"); fy(); }if($act=="list") { fy();}function fy() { $sql="select * from lyb order by ID desc"; //echo $sql;$result=$conn->query($sql);}
写在外面,$con 就是全局变量了。在函数体内当然不可用啦。改成这样
function fy() {
global $conn;
$sql="select * from lyb order by ID desc";
//echo $sql;
$result=$conn->query($sql);}
在里面包含,如果担心重复包含,可以用 require_once('conn.php'); 来避免。