Cannot return correct result from SQL query in PHP


I have created a table and there are some data inside

however, by using the following link, i always get the result of {"flag":"0","msg":"Incorrect id."}, no matter the id is correct or not

i wondering where the problems happen? thanks

    <?php session_start();

 //Connect to database from here
    $link = mysql_connect('****', '****', '****'); 
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    //select the database | Change the name of database from here

//get the posted values

//now validating the username and password
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";

printf("Select returned %d rows from tbl_user.\n", $result->num_rows);

//if username exists
    //compare the password
        // Return success message
        $message = array("flag" => "1", "msg" => "Login successfully.");
        echo json_encode($message);
         //Regenerate session ID to prevent session fixation attacks

        //now set the session from here if needed
        //$name_show=$member['first_name'].' '.$member['last_name'];
            //Write session to disc

        // Return error message
        $message = array("flag" => "0", "msg" => "Incorrect password.");
        echo json_encode($message);
else    //if username not exists
{       // Return error message
        $message = array("flag" => "0", "msg" => "Incorrect id.");
        echo json_encode($message);



It seems

$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";

result is 0 if you don't get any db connection error. Try to print$user_name to see if it is in your DB table first.

People are also looking for solutions to the problem: Can someone please clarify how MySQL's TIMESTAMP is used in conjunction with PHP's DateTime class?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.