Maison > Article > développement back-end > 为何调用函数时必须把数据库连接文件放在函数体里面
为什么调用函数时必须把数据库连接文件放在函数体里面
像下面这个程序:
<? <br /> header("Content-type: text/html; charset=gb2312"); <br /> <br /> $act=$_GET["act"];<br /> <br /> if($act=="del") { //删除记录<br /> $id =$_GET["id"];<br /> require('conn.php');<br /> $conn->query("delete from lyb where id=$id");<br /> fy(); }<br /> <br /> if($act=="list") { fy();}<br /> <br /> function fy() {<br /> require('conn.php');<br /> $sql="select * from lyb order by ID desc";<br /> //echo $sql;<br /> <br /> $result=$conn->query($sql);}
<? <br /> header("Content-type: text/html; charset=gb2312"); <br /> require('conn.php');<br /> $act=$_GET["act"];<br /> <br /> if($act=="del") { //删除记录<br /> $id =$_GET["id"];<br /> $conn->query("delete from lyb where id=$id");<br /> fy(); }<br /> <br /> if($act=="list") { fy();}<br /> <br /> function fy() {<br /> $sql="select * from lyb order by ID desc";<br /> //echo $sql;<br /> <br /> $result=$conn->query($sql);}