Mysql Query not returning my results

Garth Baker

If I run this with the query

"SELECT * FROM users"; 

It returns my result. But as soon as I run this

$username = $_POST['username'];
$password = $_POST['password'];

$login = "SELECT * FROM users WHERE name= ".$username." AND password= ".$password."";

it doesn't.

If I run it in Mysql workbench without the variables it works. If I run echo the $_POST values they come through correctly. I am stumped as to what I'm doing wrong PLEASE!! help me. I also ran my code through https://phpcodechecker.com/ and it cant see any errors in my code. This is the full function.

function login($username,$password){
global $db_conn;
$conn = new mysqli($db_conn['servername'], $db_conn['username'], $db_conn['password'], $db_conn['dbname']);
$username = $_POST['username'];
$password = $_POST['password'];

$login = "SELECT * FROM users WHERE name= ".$username." AND password= ".$password."";
    $login_result = $conn->query($login);

    if ($login_result->num_rows > 0) {

        $output = array();
        while($row = $login_result->fetch_assoc()) {
            $output[] = $row;
            echo "".$row['name']."-".$row['password']."<br>";
        }
        } else {
            echo "Invaild Login Details!"."<br>" ;
            $conn->close();
            return false;
        }
}

Every time it says "Invalid Login Details!" But I know their is one result that gets returned.

What am I doing wrong?

Anandhu Nadesh

change the query like this

$login = "SELECT * FROM users WHERE name= '$username' AND password= '$password'";

note: this method is prone to sql injection attacks. try prepared statements to avoid it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my SQL Month query not returning any results?

MySQL - Query not returning proper results

MySQL cumulative sum query not returning expected results

MYSQL JSON select query returning different results each time it is run

Mysql select query "order by" not returning results as expected

Why isn't my SQL query returning any results in hibernate?

Why is my query returning incorrect results?

Mysql query returning too many results

pymongo query not returning results

Executing query through pymysql not returning the same results as running in MySQL

MySQL query with LEFT JOIN not returning empty results

PHP with MySQL query not returning expected results

Why is my graphQL query returning results without `where` filters?

Why is my query returning far to many results?

Why is my Django filter query using dates not returning expected results?

Nodejs mysql query returning strange results instead of records

MySql Select query returning erroneous results

MySQL query returning more results than it should

MySQL Query is returning additional results

mySQL query returning 0 results using UNIX_TIMESTAMP

MySQL Query not returning any results

PHP MySQL better strategy for returning query results

Exact query in mysql.connector and regular MySQL not returning same results

Why is my PHP code returning incorrect results from a MySQL query?

MySQL Query Not Returning Results With Dual Params

Why is my stored procedure query returning extra results?

My MariaDB MYSQL query is returning results when it shouldn't

Ajax query not showing mysql results in my div

Why is my query not returning proper results?