Heim >Backend-Entwicklung >PHP-Tutorial >这个不知道错哪了7,8行报错
这个不知道哪里错了7,8行报错
include("includes/config.php");
//第 7,8 行报错
lei(0);
$ii=0;
function lei($belongs_id){
$sql=mysql_query("select * from pro_type where belongs_id='".$belongs_id."'",$conn);
while($rs_info=mysql_fetch_array($sql)){
//大类
if($belongs_id==0){
echo "大类";
}else{
echo "小类";
}
$ii=$ii+1;
lei($rs_info["id"]);
$ii=$ii-1;
}
}
?>
------解决方案--------------------
发现是变量作用域的问题。函数里的变量是局部变量,$conn要想使用全局变量,就得声明为global
------解决方案--------------------
<?include ("includes/config.php");//第 7,8 行报错lei(0);$ii=0;function lei($belongs_id){// 解决方法1, 不填$conn$sql=mysql_query("select * from pro_type where belongs_id='".$belongs_id."'");// 解决方法2, 以参数传递,或声明为全局// global $conn;// $sql=mysql_query("select * from pro_type where belongs_id='".$belongs_id."'", $conn);while($rs_info=mysql_fetch_array($sql)){//大类if($belongs_id==0){echo "大类";}else{echo "小类";}$ii=$ii+1;lei($rs_info["id"]);$ii=$ii-1;}}?><br><font color="#e78608">------解决方案--------------------</font><br>难道要<br>global $conn ;<br>$sql=mysql_query("select * from pro_type where belongs_id='".$belongs_id."'",$conn);<br><div class="clear"> </div>