0

I'm using this simple php code to encode the MySql query result in json format. But Don't know why it is not giving me the desirable output. I'm actually trying to get the employee details by entering their 'employee_number'.

<?php
require('DB_Connect.php');
require('config.php');
// check for post data
/*
 if (isset($_POST["employee_number"])) {
 $employee_name = $_POST['employee_number'];
 */
 //get a employee from employee_info table


$result = mysql_query("SELECT *FROM employee_info WHERE employee_number =9876543210");


if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);
    echo("Success !! Yoo");

    $employee = array();
            $employee["employee_number"] = $result["employee_number"];
    $employee["employee_name"] = $result["employee_name"];
    $employee["flag"]=$result["flag"];

        // success
        $response["success"] = 1;

        // user node
        $response["employee"] = array();

        array_push($response["employee"], $employee);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no employee found
        $response["success"] = 0;
        $response["message"] = "No employee found";

        // echo no users JSON
        echo json_encode($response);
    }
 }else {
    // no employee found
    $response["success"] = 0;
    $response["message"] = "No employee found";

    // echo no users JSON
    echo json_encode($response);
}
/*} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
*/
// echoing JSON response
echo json_encode($response);
?>
6
  • what output you want or what error you are getting? Commented Dec 20, 2013 at 10:02
  • I'm getting this output {"success":0,"message":"No employee found"} {"success":0,"message":"No employee found"} Commented Dec 20, 2013 at 10:04
  • above is your desired output?if yes whats your current output? Commented Dec 20, 2013 at 10:05
  • This ofcourse is not my desired output. I want the output in JSON format. Showing me the name of the employee. Commented Dec 20, 2013 at 10:06
  • Are you sure you have information under certain employee number in your database? Also, mysql_* is deprecated, use MySQLi or PDO and variable binding. Commented Dec 20, 2013 at 10:07

1 Answer 1

1

mysql_query is deprecated. Use mysqli or PDO instead.

make sure you format your query properly *note the spaces $result = mysql_query("SELECT * FROM employee_info WHERE employee_number = 9876543210");

you could also just echo the $result as your employee array doesn't seem to do anything.

echo json_encode($result);

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

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.