Heim  >  Artikel  >  Backend-Entwicklung  >  新手问个函数的有关问题

新手问个函数的有关问题

WBOY
WBOYOriginal
2016-06-13 09:59:07751Durchsuche

新手问个函数的问题
function del_news($nid)
{
  $selnews_str = "select n_class,n_image,n_pname,n_path from news where n_id=".$nid;
  $selnews_result = mysql_query($selnews_str,$con);
  $selnews_row = mysql_fetch_array($selnews_result);
  $class = $selnews_row["n_class"];
   
  if($selnews_row)
  {
  echo("有内容");
  }
  else
  {
  echo("无内容");
  }
   
  echo($nid);
}

if($_GET["nid"] != null || $_GET["nid"] != "")
{
  del_news($_GET["nid"]);
}

以上是代码,如果把del_news里的代码放在外面的话就显示有内容,放在函数里调用的话就无内容,参数是正确的。这是什么原因呢?

------解决方案--------------------

PHP code
function del_news($nid){  }$selnews_str = "select n_class,n_image,n_pname,n_path from news where n_id=".$nid;  $selnews_result = mysql_query($selnews_str,$con);  $selnews_row = mysql_fetch_array($selnews_result);  $class = $selnews_row["n_class"];     if($selnews_row)  {  echo("有内容");  }  else  {  echo("无内容");  }     echo($nid);if($_GET["nid"] != null || $_GET["nid"] != ""){  del_news($_GET["nid"]);}<br><font color="#e78608">------解决方案--------------------</font><br>错误信息是什么?<br><br>猜测:<br>可能是函数作用域的问题。 你的数据库连接句柄是全局的。在函数里面操作数据被认为是没有连接数据库的。<br><font color="#e78608">------解决方案--------------------</font><br>$selnews_result = mysql_query($selnews_str,$con);<br>这里的$conn是局部变量<br>没有声明Global。<br>可能是这个原因造成的。<br>把数据库连接句柄加入函数内部试下。<br><br><font color="#e78608">------解决方案--------------------</font><br>$selnews_result = mysql_query($selnews_str);<br><font color="#e78608">------解决方案--------------------</font><br>执行顺序! 你放在函数里边 怎么调用啊!<br><font color="#e78608">------解决方案--------------------</font><br>你那种可以解决。 还有版主那样也行。<div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn