0

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

7
  • 5
    $user='Admin'; and $user='USER';. The second one is overwriting the other. Commented Apr 23, 2016 at 13:32
  • Are you sure $user is a valid user and that the query shall return something with it? Commented Apr 23, 2016 at 13:36
  • 5
    What @Fred-ii- is pointing out is that you used the same variable name ($user) for the user name in the SQL query and the user name you use to connect to the database in mysql_connect(). Also please note that mysql IS DEPRECATED AND NOT SECURE. USE mysqli or PDO Commented Apr 23, 2016 at 13:38
  • Anyhow @Fred-ii- is right, in the first script you're overwriting $user value Commented Apr 23, 2016 at 13:38
  • $query = "SELECT * FROM AppLogin WHERE Username ='$user' "; Try this one. Commented Apr 23, 2016 at 13:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the downvote. It's a change from seeing green all the time :-)
0

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

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.