0

I have the following code with my php(updated):

$sql = "INSERT INTO uac_user (user_name, user_password, create_time, lastupdateTime) VALUES ('$usernmae', '$password', NOW(), NOW())";

if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id; //get new id
    $records = array();  //select role
    if($result = $conn->query("SELECT role_id FROM uac_role WHERE `role_name` = '$role';")){
        if($result->num_rows){
            while ($row = $result->fetch_object()){
                $records = $row;
            }
            $dateResult=(string)json_encode($records);
            echo ($dateResult);
            echo ($dateResult['role_id']);
            // $sql2 = "INSERT INTO uac_mapping (role_id, user_id) VALUES ('$dateResult['role_id']', '$last_id')"; //insert mapping
            // if ($conn->query($sql2) === TRUE) {
                // echo "success"; 
            // }
            // else {
                // echo "Error: " . $sql . "<br>" . $conn->error;
            // }
        }
        else $records = 'no data';
    }
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
die();

the first echo return [{"role_id":"4"}]

but the second return [

what I need for the second one is 4

what is the problem about my code?

1
  • Add the $records array exactly as it is. It's easier to pinpoint the problem that way. Commented Sep 27, 2017 at 8:27

2 Answers 2

1

I think you have made a common mistake of confusing what JSON is and is not. JSON is a way of representing some data in a string, for transfer to another system, where it will be turned back into something else for use. JSON is not a type of object that you can use directly to extract data.

Look carefully at this line:

$dateResult=(string)json_encode($records);

The result of this is that $dateResult is a string - you even have an extra (string), but even without that, the manual page for json_encode makes clear that you will always have a string:

Returns a string containing the JSON representation of the supplied value.

You then run this:

$dateResult[role_id]

You are trying to look up role_id inside a string. But a string is just a bunch of symbols, it doesn't know what "role_id" is. You had structured data in $records, it's there that you want to look up role_id.

If $records is an array, what you want is $records['role_id'] (note the quotes around 'role_id'; role_id would be the name of a constant, not a string).

If $records is an object, what you want is $records->role_id (no quotes this time, because ->role_id is more like a variable name than a string).

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

3 Comments

I just updated the code and the $records is an array I have tried both of 'role_id'; role_id but it doesn't work
@WaiHung Read my answer again. You do not need json_encode here, you do not need $dateResult, you do not need anything to do with JSON. Also, the function name "fetch_object" should give you a clue that what it will return is in fact an object.
I just use echo ($records->role_id); and it works. Thank you so much
0

Your json is probably an array of objects, so you can loop through them and use something like:

echo $dateResult[$counter]->role_id;
//$counter will be your loop incrementer;

1 Comment

but it does not return 4

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.