Because the number of rows returned only makes sense with a particular result set. However, for a SELECT query mysqli_query() can return a result resource (representing a result set) or false (a boolean). Consequently, you should check, e.g.,
$conn = mysqli_connect("localhost", "root", "", "cars");
if ($result = mysqli_query($conn, "select * from users where name='" . $username . "'")) {
if (mysqli_num_rows($result) == 0) {
/////insert the data if not existed/////
// ...
Incidentally, it is better to write:
$conn = mysqli_connect("localhost", "root", "", "cars");
if ($result = mysqli_query($conn, "SELECT COUNT(*) FROM users WHERE name='" . $username . "'")) {
$row = $result->fetch_assoc();
if ($row['COUNT(*)'] == 0) {
/////insert the data if not existed/////
// ...
This way you only retrieve what you want. An even better way would be to attempt to insert the username. With the name column declared UNIQUE, the database server will report a UNIQUE constraint error that you can check for and thus handle.