新手问个函数的有关问题

WBOY
Release: 2016-06-13 13:51:43
Original
863 people have browsed it

新手问个函数的问题
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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!