0

I know this might be a duplicate on:
php - MYSQLI_NUM_ROWS ALWAYS RETURNING 0
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.


index.html

<html>
    <head><title>GAS - Give Away System - Create User</title></head>
    <body>
        <script src="jquery.js"></script>
        <input type="text" placeholder="Username" id="uname" /><br><br>
        <p id="errors"></p>
        <script>
            $(document).ready(function(){
                $.ajaxSetup({cache:false});
                setInterval(function(){
                    $("#errors").load("php/errorchecking.php?c=username&v="+document.getElementById('uname').value);
                }, 500);
            });
        </script>
    </body>
</html>

php/errorchecking.php

<?php
    $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>';

        if($_GET['c']==="username"){
            if($_GET['v']===null){
                echo "Sorry, this username is empty! Please write a username!";
            }else{
                // 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";
                }else{
                    echo "Username avaible!"; //This is line 17
                }
            }
        }
    }else{
        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 User15, so what it should display is "Sorry, this username is already in use! Please choose something else"

Thanks for taking time helping me! Cecilie.

5
  • $query = "SELECT username FROM users WHERE username='".$_GET['v']."'"; Commented Jul 25, 2016 at 11:18
  • Hahaha, thank you very much @Anant . That was very helpfull! If you post this as an answer I will use it :) Commented Jul 25, 2016 at 11:21
  • 2
    I will accept it in 8 minutes, I have to wait before i can put it to the answer @PraveenKumar Commented Jul 25, 2016 at 11:23
  • @Anant Partial solution. :) I gave a full explanation. That counts. :) You didn't explain why it happens, and we both have a difference of 12 seconds, which I used to explain it well. An answer should not be just do this and you'll get it, will be like you don't teach the person. Commented Jul 25, 2016 at 11:38
  • A partial solution is that which solves only some part of the problem not the complete problem. explanation only add information about the logic, it didn't make a solution partial or full. Commented Jul 25, 2016 at 12:53

2 Answers 2

3

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']."'";

Note:

  • `` - For columns.
  • '' - For values.
Sign up to request clarification or add additional context in comments.

Comments

0

You have to remove '(single quotes) around your column-name from your query like below:-

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

Note:- Instead of '(single quotes) use back-ticks. because single-quotes(') used for values and back-ticks used for column-name as well as table-name too.Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.