I get the 500 error when I try to return a value from PHP object/array. But it's ok return all the object

php try catch
php array
php try catch not working
php print array
php try catch fatal error
php exception handling best practices
php error vs exception
php throw exception with code

I'm trying to create a simple application in Laravel that sends and gets comments from the database.

I'm using both PHP and JS with AJAX. But when I try to get the full comment object:

PHP

    public function UpdateComment(Request $request){
        $id = $request->id;
        $jsonCode = DB::table('comments')->where('id', $id)->get();
        $comment = json_decode($jsonCode);
        return $comment
    }

Works well:

[{…}]
  0:
   author_id: 6
   comment_date: "2018-09-15 09:53:01"
   comment_text: "23423434234"
   history: ""
   id: 60
   last_update: "2018-09-15 00:00:00"
  >proto__: Object
  length: 1
>proto__: Array(0)

This is exactly what I expected to see, the full PHP object. But when I try to return or simply use a single attribute of this same object, I got a 500 error...

return $comment->id;

.

POST http://laravelhost/update-comment 500 (Internal Server Error)

I'm a beginner in PHP, so it should be a very simple error.


You are getting error 500 because json_decode() returns an array, not an object. So to access your data use array syntax, instead of object operator:

return $comment['id']

Host Your Web Site In The Cloud: Amazon Web Services Made Easy: , You are getting error 500 because json_decode() returns an array, not an object. So to access your data use array syntax, instead of object operator: Success! You'll only get the latest updates from us at Slothparadise. Something went wrong.


Try this:

public function UpdateComment(Request $request){
    $id = $request->id;
    $record = DB::table('comments')->where('id', $id)->get();
    return $record;
}

You don't need to json_decode() the return value of DB::table()... call.

Pro Couchbase Server, If S3 is unable to store the object, it will return an HTTP 500 (internal server error) code the proper response is to make several attempts with increasing time delays $try = 1; $sleep = 1; do { $res = $s3>create_object($bucket, array( '​filename' chapter_04/upload_file.php (excerpt) #!/usr/bin/php <?php error reporting (E  Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See return for more information. If the return is omitted the value NULL will be returned.


To retrieve a single row you should use first() instead of get()

$comment = DB::table('comments')->where('id', $id)->first();
echo $comment->id;

https://laravel.com/docs/5.7/queries

PHP Object-Oriented Solutions, keyAlreadyExists) { // Document was changed between the get and set. return rantback, retries - 1, updateCallback); } else return updateCallback(500, {}) which is a JSON array, push the new rantback into the array, and then attempt to store If the error object isn't NULL, and the error code equals keyAlreadyExists, the  I know this question is old, however I ran into this problem on Windows 8.1 while trying to use .htaccess files for rewriting. My solution was simple, I forgot to modify the following line in httpd.conf


You can either use

$comment = DB::table('comments')->where('id', $id)->first();
return $comment->id;

or

$comment = DB::table('comments')->where('id', $id)->get();
return $comment[0]->id;

If you are using json_decode after fetching data, object will converted to array. So, you have to use $arrayname['keyname'] for fetching data.

Exception handling - PHP: Exceptions, Of course, if you get a parse error, it means there's a syntax error in your code. like this (or use test_validator_04.php): $val->checkTextLength('comments', 10, 500); a try block because it is liable to throw an exception if something goes wrong. The function returns an array containing the filtered and validated input,​  Most of the time, "wrong" means an issue with the page or site's programming, but there's certainly a chance that the problem is on your end, something we'll investigate below.


array - Manual, Each try must have at least one corresponding catch or finally block. The thrown object Trying to throw an object that is not will result in a PHP Fatal Error. Additionally, if the finally block also contains a return statement, the value from the finally block is returned. public function getTrace(); // An array of the backtrace() If you try to visit a website and see a “500 Internal Server Error” message, it means something has gone wrong with the website. This isn’t a problem with your browser, your computer, or your internet connection. It’s a problem with the site you’re trying to visit.


var_export - Manual, Syntax "index => values", separated by commas, define index and values. index may Returns an array of the parameters. If you need to add an object as an array key, for example an object from From a PHP perspective these are O(1) (​without getting into what's going on in the Other versions throws a notice error. Case A: program returns value from try-block only. Compile-time error: This method must return a result of type int . Case B: program returns value from catch-block only. Compile-time error: This method must return a result of type int . Case C: program returns value from try-catch-finally blocks; but contains some statements after finally-block


jQuery in easy steps: Create dynamic web pages, 7.3.0, Now exports stdClass objects as an array cast to an object ( (object) variable passed to var_export, the return value will be a typecasted as string. Try this function instead of var_export($GLOBALS) or var_dump($GLOBALS) (​500 lines skipped) . Moreover, you can have an error on an simple recursive array: If you don't stringify your response it becomes "[object Object]". Not very helpful. Context then takes this string result and assigns it to the errorMessage property of the object it returns to API Gateway. So API Gateway gets something that looks like {errorMessage: "<your stringified response"}.