0

Webservice code:

function login($uname)
        {   

            $id=1;
            $link = mysql_pconnect("localhost", "root", "root") or die("Could not connect");
            mysql_select_db("sparq",$link) or die("Could not select database");
            $sql=mysql_query("select username,password from user_login where user_id=1");
            //$result = mysql_query($query);
            $arr = array();
            while($obj = mysql_fetch_object($sql)) 
                {
                    $arr[] = $obj;
                    }
           // $obj = mysql_fetch_object($sql);
           header("Content-type: application/json");
           echo json_encode($arr);
        }

Code from client:

$url="http://localhost/web.php";
if (isset($_POST['Login']))
    {

            $ch = curl_init($url); // Initialize a CURL session
            curl_setopt($ch, CURLOPT_HEADER, 0); // options for a CURL transfer
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,"username=".$username );
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch); // Perform a CURL session
            curl_close($ch); 
            $arr =array();
            $arr=json_decode($data,true);
            echo 'I am here';    //echo1
            echo $data;   //echo2

            echo $arr[0]->username; //echo3

I am getting the below output:

I am here //echo1
[{"username":"[email protected]","password":"asdf123"}]  //echo2
Notice: Trying to get property of non-object in F:\xampp\htdocs\webtest\login.php on line 38  //echo3

2 Answers 2

3

This:

echo $arr[0]->username;

Should be:

echo $arr[0]['username'];

And this:

$arr = array();
while($obj = mysql_fetch_object($sql)) {
  $arr[] = $obj;
}

Should be:

$arr = array();
while($row = mysql_fetch_assoc($sql)) {
  $arr[] = $row;
}

You can't send php objects over json. What javascript (and json) call an object is an associative array in php.

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

4 Comments

But the below will return undefined constant username error "\n" echo $arr[0]['username'];
@akhil-n-k If you are getting that error you forgot the quotes around 'username'.
still it's not printing the value of username
@akhil-n-k Do print_r($arr); and post the output.
0

Replace: echo $arr[0]->username; by $arr[0]['username'];

4 Comments

One more thing: The initial code I posted is working well in my friend's m/c and I am getting the correct output
Tell me what do you have by inserting var_dump($arr); before "echo3"
It's because you have to use UTF-8 encoding. Encode your file script in UTF-8 and your db table fields or use $arr=json_decode(utf8_encode($data),true);
Still no hope. I am getting 4 as output from json_last_error(). I added that below the line $arr=json_decode($data,true);

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.