Home  >  Article  >  Backend Development  >  这个不知道错哪了7,8行报错

这个不知道错哪了7,8行报错

WBOY
WBOYOriginal
2016-06-13 13:40:20875browse

这个不知道哪里错了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
------解决方案--------------------

PHP code

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn