Cecilie Olsen Cecilie Olsen - 1 year ago 94
PHP Question

php: mysqli_num_rows is returning 0

I know this might be a duplicate on:


php - mysqli_num_rows() is always returning 0

The thing is none of them is giving me an answer for my problem.

This is an example code of my code with the same syntaxes as I used.


<head><title>GAS - Give Away System - Create User</title></head>
<script src="jquery.js"></script>
<input type="text" placeholder="Username" id="uname" /><br><br>
<p id="errors"></p>
}, 500);


$con = mysqli_connect('localhost', 'root', '', 'gas');
if(isset($_GET['c']) && isset($_GET['v'])){
echo 'Current value: ', $_GET['v'], '<br>This value is set for: ', $_GET['c'], '<br><br>';

echo "Sorry, this username is empty! Please write a username!";
// I know this is open for SQL Injection, it's not in my worries.
$query = "SELECT `username` FROM `users` WHERE `username` ='{$_GET['v']}'";
$sql = $con->query($query);

if(mysqli_num_rows($sql) > 0){
echo "Sorry, this username is already in use! Please choose something else";
echo "Username avaible!"; //This is line 17
echo 'This is an invalid form!';

Now lets say I have a username in my table called User15, and someone's input is the exact same it will display the message
"Username available!"
from php/errorchecking.php Line:17

Why does it do that? Since it already is a user there called
, so what it should display is
"Sorry, this username is already in use! Please choose something else"

Thanks for taking time helping me! Cecilie.

Answer Source

That's a wrong way of query syntax. You need to use back-ticks and not 's:

$query = "SELECT username FROM `users` WHERE 'username'='".$_GET['v']."'";

Change it to:

$query = "SELECT `username` FROM `users` WHERE `username`='".$_GET['v']."'";


  • `` - For columns.
  • '' - For values.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download