Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQLI Count Error
    text
    copied!<p>I have two different functions:</p> <pre><code>function user_count(){ global $db; $query = mysqli_query($db,"SELECT COUNT(user_id) FROM users"); if($result = $query){ $user_count = mysqli_num_rows($result); mysqli_free_result($result); return $user_count; } } </code></pre> <p>In this function, I query the database to tell me how many users there are, and in PHP I use user_count(). The result of this function is 1. Which is correct, because I only have only 1 users in the database at the moment. However when I use the same code for a different function such as:</p> <pre><code>function user_exists($username){ global $db; sanitize($username); $query = mysqli_query($db,"SELECT COUNT(user_id) FROM users WHERE username=".$username) or die ("Doesn't work"); if($result = $query){ $user_exists = mysqli_num_rows($result); mysqli_free_result($result); return $user_exists; } } </code></pre> <p>Not to mention I have also tried to comment out the sanitize($username). But even with it commented out I still am getting an error.</p> <p>And I use the function user_exists("superadmin"), I get the error "Doesn't work". I am wondering what am I doing wrong in the second function where the first function would return the number of rows, where as second function would return an error.</p> <p>I know for certain that:</p> <ol> <li>Database is connecting</li> <li>String sanitization is working</li> <li>User does exists on the database</li> </ol> <p>If would much appreciated if you reply using the procedural style.</p>
 

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