0

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!

1
  • Have you tried like this... $result = mysql_query("SELECT * FROM population WHERE gender = '" . $gender . "'"); Commented Apr 10, 2013 at 11:25

3 Answers 3

2

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"];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

$row was the issue. Changing it works great but why am I seeing only one result I should get three? Any idea on that?
1

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);

3 Comments

mysql_fetch_array($res, MYSQL_ASSOC) *
@Yogesh Suthar : $row was the issue. Changing it works great but why am I seeing only one result I should get three? Any idea on that?
@TheDevMan Because you didn't iterate the result with foreach OR while loop. See edited answer with while loop.....
0

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!

4 Comments

Nope, This is not necessary to do this way of concatenate. Inside of double qouted ("") with single quote around ('$gender'). PHP parsing this value.
@Ranjith do you have any reference to that? The rule of thumb is: between double quotes, vars expand, between single quotes, they don't. However, I tested, and it works as you say, but I can't find any documentation on that.
It's wrong. PHP doesn't parse the variable when around the single quotes like this '$gender'. But it can parse when like this way of assignment or execution " ........ '$gender' ......" . It's my humble request to you try a simple PHP program and check it out..
Indeed, I tried it in a simple PHP prog, and noticed it does work. But indeed only when the single quotes are contained by double quotes.

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.