HerrJulle
HerrJulle

Reputation: 25

Sql won't get values WHERE

Okay, so I'm trying to create an app for iOS.

My problem is that, what I think, when using JSON and combine it with sql, somehow it does not work.

So this is my code

<?php
$user='Admin';
echo $user;


    $host='HOST';
    $user='USER';
    $password='PASSWORD';



    $connection = mysql_connect($host,$user,$password);

    if(!$connection){
        die('Connection Failed');
    }
    else{
        $dbconnect = @mysql_select_db('DATABASE', $connection);

        if(!$dbconnect){
            die('Could not connect to Database');
        }
        else{
            $query = "SELECT * FROM AppLogin WHERE Username = '".$user."' ";
            $resultset = mysql_query($query, $connection);

            $records= array();

            while($r = mysql_fetch_assoc($resultset)){
                $records[] = $r;
            }

            echo json_encode($records);
        }
    }
?>

The output I get is

Admin[]

If I do it like this

<?php


        $host='HOST';
        $user='USER';
        $password='PASSWORD';



        $connection = mysql_connect($host,$user,$password);

        if(!$connection){
            die('Connection Failed');
        }
        else{
            $dbconnect = @mysql_select_db('DATABASE', $connection);

            if(!$dbconnect){
                die('Could not connect to Database');
            }
            else{
                $query = "SELECT * FROM AppLogin WHERE Username = 'Admin' ";
                $resultset = mysql_query($query, $connection);

                $records= array();

                while($r = mysql_fetch_assoc($resultset)){
                    $records[] = $r;
                }

                echo json_encode($records);
            }
        }
    ?>

I get the output I want:

[{"Username":"Admin","Password":"Password"}]

Why is the sql not working when I'm using WHERE = '".$value."'

Albin

Upvotes: 0

Views: 70

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

As I mentioned in comments:

"$user='Admin'; and $user='USER';. The second one is overwriting the other."

Therefore, you need to rename one of those.

Plus, you should really switch to PDO with prepared statements or mysqli_* with prepared statements if your server supports them, as the mysql_* functions are deprecated.

Upvotes: 2

Sachin Vairagi
Sachin Vairagi

Reputation: 5334

Try this $query = "SELECT * FROM AppLogin WHERE Username = '$user' ";

Upvotes: 0

Related Questions