Note that there are some explanatory texts on larger screens.

plurals
  1. POissue with global in php
    primarykey
    data
    text
    <p>Basically I have the function below called 'article_exists', this allow me to check if the article exist in my database. The condition is just basic and thorough. I am also using ajax to retrieve the result. Everything is working perfectly fine,the query is working as well as my ajax's function. But whenever I try to implement any 'global' variable inside this 'article_exists' function. My ajax's function suddenly stop working, and I notice that I have to refresh the page to see the result.</p> <pre><code>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; } </code></pre> <p>The goal that I am trying to accomplish is the code below. This time, not only my ajax's function stop working but this 'article_exists' function as well.</p> <pre><code> 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; } </code></pre> <p>The variable "$num" has a value = 'first' and I am trying to append this inside the query. To test whether this variable actually works, I've echoed it outside the function and it does work. I even tried to see whether the whole query is working by creating a new function called 'getResult' and it does work but not on my 'article_exists()' function. The query test function is below</p> <pre><code> 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-&gt;getMessage(); $e-&gt;getCode(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload