TheDevMan
TheDevMan

Reputation: 5954

Retrieve data from database using PHP

I am trying to retrieve data from mysql using PhP. I am just testing my PHP file. If this works then I will start working on getting those results to my Android App.

The PHP results shown are always null. I can confirm that data is available in the database.

DATABASE has table called:

population - Fields are: id, gender, city, state

The results are always:

{"success":1,"data":[{"id":null,"gender":null,"city":null,"state":null}]}

I am not sure what is wrong in the code:

 <?php
  include("db_config.php");

  $response = array();

  $gender = 'Male';

  // get a product from products table
  $result = mysql_query("SELECT * FROM population WHERE gender = '$gender'");

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

        $result = mysql_fetch_array($result);

        $data = array();

        $data ["id"] = $row["id"];
        $data ["gender"] = $row["gender"];
        $data ["city"] = $row["city"];
        $data ["state"] = $row["state"];

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

        $response["data"] = array();
        array_push($response["data"], $data);
        echo json_encode($response);
       }
    else 
     {
        $response["success"] = 0;
        $response["message"] = "No data found";

        echo json_encode($response);
     }
    }
  else 
   {

    $response["success"] = 0;
    $response["message"] = "No data found";
    echo json_encode($response);
     } 
  ?>

Can someone help me out with this php code? I have been working on this for last three days couldn't get any solution?

Thanks!

Upvotes: 0

Views: 460

Answers (3)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

change this

$result = mysql_fetch_array($result);

to

$row = mysql_fetch_array($result);

use below code to get the all data

while($row = mysql_fetch_array($result)) {
    $data = array();
    $data ["id"] = $row["id"];
    $data ["gender"] = $row["gender"];
    $data ["city"] = $row["city"];
    $data ["state"] = $row["state"];
    // success
    $response["success"] = 1;
    $response["data"][] = array();
    array_push($response["data"][], $data);
}
echo json_encode($response);

Upvotes: 1

Borniet
Borniet

Reputation: 3546

A variable in PHP is not evaluated when placed between single quotes.

"SELECT * FROM population WHERE gender = '$gender'"

Try concatenation:

$query = "SELECT * FROM population WHERE gender = '" . $gender "'";
mysql_query($query);

PS: Besides this error, I have to tell you it is best not to use mysql_* functions anymore, use mysqli or pdo!

Upvotes: 0

Ranjith
Ranjith

Reputation: 2819

Try it Now,

if (mysql_num_rows($result) > 0) 
     {

        $row = mysql_fetch_array($result);  // wrong initialize to the result value

        $data = array();

        $data ["id"] = $row["id"];
        $data ["gender"] = $row["gender"];
        $data ["city"] = $row["city"];
        $data ["state"] = $row["state"];
    }

Upvotes: 2

Related Questions