php中的全局问题

时间:2013-03-15 06:31:21

标签: php ajax function global

基本上我有下面的函数'article_exists',这允许我检查文章是否存在于我的数据库中。条件是基本和彻底的。我也使用ajax来检索结果。一切都工作得很好,查询工作和我的ajax功能一样。但每当我尝试在'article_exists'函数中实现任何'全局'变量时。我的ajax的功能突然停止工作,我注意到我必须刷新页面才能看到结果。

function article_exists($article_id){
        $article_id = (int)$article_id;
        return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM first_article WHERE `id` = $article_id"), 0) == 0) ? false: true;
    }

我想要完成的目标是下面的代码。这一次,不仅我的ajax功能停止工作,而且这个'article_exists'功能也是如此。

 function article_exists($article_id){
global $num;//Applying the global variable and using it on the query
            $article_id = (int)$article_id;
            return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM ".$num."_article WHERE `id` = $article_id"), 0) == 0) ? false: true;
        }

变量“$ num”的值为'first',我试图将其追加到查询中。为了测试这个变量是否真的有效,我在函数之外回应了它,它确实有效。我甚至试图通过创建一个名为'getResult'的新函数来查看整个查询是否正常工作,它确实有效但不在我的'article_exists()'函数上。查询测试功能在

之下
 function getResult() {
        global $num;
       try {
          $q = mysql_query("SELECT COUNT(`id`) FROM ".$num."_article WHERE `id` = $article_id");

          if ($q === FALSE)
             throw new Exception(mysql_error(), mysql_errno());

          // Do stuff with the query results here.
       } catch(Exception $e) {
          // Do the error handling here
         $e->getMessage();
         $e->getCode();
       }
    }

0 个答案:

没有答案