2

I have this piece of PHP code which should accept a number sent from an Android app and search the equivalent to that number in the database (in the other field) and send the result to the Android app.

But I get this error:

Object of class stdClass could not be converted to string in D:\xampp\htdocs\ \b.php

in this line:

mysql_query(" select  stock_account from  Stock where  phone_id=   (string)$data ") 

This is the complete code:

if( isset($_POST["json"]) )
{
    $data = json_decode($_POST["json"]);

    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("dbname") or die(mysql_error());

    $datatwo = mysql_query(" select  stock_account from  Stock where   id=   (string)$data ") or die(mysql_error()) ;  

    echo $datatwo;
}
5
  • 1
    what is the value of $_POST['json'] ? Commented Jan 12, 2016 at 9:05
  • 1
    Don't use mysql_* functions. stackoverflow.com/questions/12859942/… Commented Jan 12, 2016 at 9:06
  • @ChrisBanks the value of $_POST['json'] is the sent number from the android Commented Jan 12, 2016 at 9:10
  • what is the raw value? var_dump($_POST['json']) for us, or var_dump($data) for us Commented Jan 12, 2016 at 9:10
  • Be sure to accept an answer using the checkbox if it resolves your question, or optionally up vote an answer too. Commented Jan 12, 2016 at 9:39

1 Answer 1

1

The first thing is that json_decode generates an object. The second thing you want to is a value from the $data variable so we use $data->id to get the id from the JSON. I'm casting it to an integer like this $id = (int)$data->id; and storing it in a new variable. Now in the query you can pass the $id variable to fetch your row.

If you want a different value off your $data variable you will need to change $data->id to something else.

<?php

if (isset($_POST["json"])) {
    $data = json_decode($_POST["json"]);

    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("dbname") or die(mysql_error());

    $id = (int)$data->id;
    $datatwo = mysql_query("SELECT stock_account FROM Stock WHERE phone_id =$id") or die(mysql_error()) ;  

    $row = mysql_fetch_assoc($datatwo);
    echo $row['stock_account'];
}

Read more here: http://php.net/json_decode

json_decode — Decodes a JSON string

Usage: mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

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

3 Comments

I get Resource id #6
Okay, now change echo $datatwo to this $row = mysql_fetch_assoc($datatwo); echo $row['stock_account']; -- I updated my post with how it should look like
Resource id #6 presents that MySQL has found a result set

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.