Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have a lot of logic problems.</p> <ol> <li><p>If the user ID is 0 (not logged in) then you kill the PHP script; nothing is returned. This is <em>not</em> the same as <code>false</code>, it will evaluate as <code>''</code> and <code>'' !== 0</code>.</p></li> <li><p>If the SQL statement fails, same problem.</p></li> </ol> <p>You need to re-factor your PHP script to either return a consistent result no matter what. Something like this:</p> <pre><code>&lt;?php $new_messages = false; if ($uid &gt; 0) { $sql = 'SELECT messagecount FROM Users WHERE uid = ? AND messagecount &gt;= 1'; $stmt = $db-&gt;prepare($sql); $stmt-&gt;execute(array($uid)); $row = $stmt-&gt;fetchObject(); if (is_object($row) &amp;&amp; intval($row-&gt;messagecount) !== intval($_POST['countOld'])) { $new_messages = true; } } header('Content-Type: application/json'); print json_encode(array( 'new_messages' =&gt; $new_messages )); </code></pre> <p>This will return a boolean (<code>true</code> or <code>false</code>) value no matter what, which you can then test against in your JavaScript:</p> <pre><code>$.post('messagecheck.php', { countOld: count }, function(result) { if (result &amp;&amp; result.new_messages) { if (result.new_messages === true) { alert('You have a new message.'); } } }); </code></pre> <p>Also, don’t use <code>mysql_</code> functions, they’re deprecated as per the warning on the PHP.net manual page: <a href="http://php.net/manual/en/function.mysql-query.php" rel="nofollow">http://php.net/manual/en/function.mysql-query.php</a>. Use <a href="http://php.net/manual/en/book.pdo.php" rel="nofollow">PDO</a> or <a href="http://php.net/manual/en/book.mysqli.php" rel="nofollow">MySQLi</a> instead.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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