{"id":2216,"date":"2021-07-09T10:05:24","date_gmt":"2021-07-09T10:05:24","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2216"},"modified":"2021-07-09T10:10:53","modified_gmt":"2021-07-09T10:10:53","slug":"fetchobject","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-pdo\/fetchobject\/","title":{"rendered":"fetchObject"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the <code>fetchObject()<\/code> method to fetch the next row from a result set and returns it as an object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-fetchobject-method'>Introduction to the fetchObject() method <a href=\"#introduction-to-the-fetchobject-method\" class=\"anchor\" id=\"introduction-to-the-fetchobject-method\" title=\"Anchor for Introduction to the fetchObject() method\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you have a <code>pulishers<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">CREATE TABLE <span class=\"hljs-keyword\">IF<\/span> NOT EXISTS publishers (\r\n    publisher_id INT AUTO_INCREMENT,\r\n    name VARCHAR(<span class=\"hljs-number\">255<\/span>) NOT <span class=\"hljs-keyword\">NULL<\/span>,\r\n    PRIMARY KEY (publisher_id)\r\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>and a <code>Publisher<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Publisher<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $publisher_id;\n    <span class=\"hljs-keyword\">private<\/span> $name;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To fetch each row from the <code>publishers<\/code> table and returns it as a <code>Publisher<\/code> object, you use the <code>fetchObject()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">fetchObject<\/span><span class=\"hljs-params\">(\n    string|null $class = <span class=\"hljs-string\">\"stdClass\"<\/span>, \n    array $constructorArgs = &#91;]\n)<\/span>: <span class=\"hljs-title\">object<\/span>|<span class=\"hljs-title\">false<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>fetchObject()<\/code> is a method of the <code>PDOStatement<\/code> class. It fetches the next row from the result set associated with a <code>PDOStatement<\/code> object and returns an object of a class. If the <code>fetchObject()<\/code> method fails, it&#8217;ll return <code>false<\/code> instead.<\/p>\n\n\n\n<p>The <code>fetchObject()<\/code> method has two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>$class<\/code> specifies the class of the object to return. If you omit it, the <code>fetchObject()<\/code> method will return an instance of the <code>stdClass<\/code> class.<\/li><li><code>constructorArgs<\/code> is an array that specifies the arguments passed to the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-constructors\/\">constructor<\/a> of the <code>$class<\/code>.<\/li><\/ul>\n\n\n\n<p>The <code>fetchObject()<\/code> maps the columns of the row with the properties of the object with the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, assign the column value to the property with the same name.<\/li><li>Second, call the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-magic-methods\/\"><code>__set()<\/code> magic method<\/a> if a column doesn&#8217;t have a property in the class with the same name. If the classs has no <code>__set()<\/code> magic method, create a public property with the value derived from the column.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-fetchobject-method-example'>PHP fetchobject() method example <a href=\"#php-fetchobject-method-example\" class=\"anchor\" id=\"php-fetchobject-method-example\" title=\"Anchor for PHP fetchobject() method example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to use the <code>fetchObject()<\/code> method to fetch a row from the <code>publishers<\/code> table and returns a <code>Publisher<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Publisher<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $publisher_id;\n    <span class=\"hljs-keyword\">private<\/span> $name;\n}\n\n<span class=\"hljs-comment\">\/\/ connect to the database<\/span>\n$pdo = <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ execute the query<\/span>\n$sql = <span class=\"hljs-string\">'SELECT publisher_id, name \n        FROM publishers \n        WHERE publisher_id=:publisher_id'<\/span>;\n\n$statement = $pdo-&gt;prepare($sql);\n$statement-&gt;execute(&#91;<span class=\"hljs-string\">':publisher_id'<\/span> =&gt; <span class=\"hljs-number\">1<\/span>]);\n\n<span class=\"hljs-comment\">\/\/ fetch the row into the Publisher object<\/span>\n$publisher = $statement-&gt;fetchObject(<span class=\"hljs-string\">'Publisher'<\/span>);\n\nvar_dump($publisher);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">object(Publisher)<span class=\"hljs-comment\">#3 (2) { <\/span>\n    &#91;<span class=\"hljs-string\">\"publisher_id\"<\/span>:<span class=\"hljs-string\">\"Publisher\"<\/span>:<span class=\"hljs-keyword\">private<\/span>]=&gt; string(<span class=\"hljs-number\">1<\/span>) <span class=\"hljs-string\">\"1\"<\/span> \n    &#91;<span class=\"hljs-string\">\"name\"<\/span>:<span class=\"hljs-string\">\"Publisher\"<\/span>:<span class=\"hljs-keyword\">private<\/span>]=&gt; string(<span class=\"hljs-number\">21<\/span>) <span class=\"hljs-string\">\"McGraw-Hill Education\"<\/span> \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a <code>Publisher<\/code> class with two properties <code>publisher_id<\/code> and <code>name<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Publisher<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $publisher_id;\n\n    <span class=\"hljs-keyword\">private<\/span> $name;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-mysql\/\">connect to the <code>bookdb<\/code> database using the <code>connect.php<\/code> object:<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$pdo = <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, execute a prepared statement that selects a publisher with id 1 from the <code>publishers<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ execute the query<\/span>\n$sql = <span class=\"hljs-string\">'SELECT publisher_id, name \n        FROM publishers \n        WHERE publisher_id=:publisher_id'<\/span>;\n\n$statement = $pdo-&gt;prepare($sql);\n$statement-&gt;execute(&#91;<span class=\"hljs-string\">':publisher_id'<\/span> =&gt; <span class=\"hljs-number\">1<\/span>]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, return a <code>Publisher<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$publisher = $statement-&gt;fetchObject(<span class=\"hljs-string\">'Publisher'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that if the <code>Publisher<\/code> class has a namespace, you need to pass a fully qualified class name. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">phptutorial<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Publisher<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">private<\/span> $publisher_id;\n    <span class=\"hljs-keyword\">private<\/span> $name;\n}\n\n<span class=\"hljs-comment\">\/\/ connect to the database<\/span>\n$pdo = <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ execute the query<\/span>\n$sql = <span class=\"hljs-string\">'SELECT publisher_id, name \n        FROM publishers \n        WHERE publisher_id=:publisher_id'<\/span>;\n\n$statement = $pdo-&gt;prepare($sql);\n$statement-&gt;execute(&#91;<span class=\"hljs-string\">':publisher_id'<\/span> =&gt; <span class=\"hljs-number\">1<\/span>]);\n\n<span class=\"hljs-comment\">\/\/ fetch the row into the Publisher object<\/span>\n$publisher = $statement-&gt;fetchObject(<span class=\"hljs-string\">'phptutorial\\Publisher'<\/span>);\n\nvar_dump($publisher);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note the first line that defines the namespace phptutorial:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">phptutorial<\/span>;\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the line that uses the fetchObject() method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$publisher = $statement-&gt;fetchObject(<span class=\"hljs-string\">'phptutorial\\Publisher'<\/span>);\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>fetchObject()<\/code> method to fetch the next row from a result set and returns it as an object of a class.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2216\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/fetchobject\/\"\n\t\t\t\tdata-post-title=\"fetchObject\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2216\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/fetchobject\/\"\n\t\t\t\tdata-post-title=\"fetchObject\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the fetchObject() method to fetch the next row from a result set and returns it as an object. Introduction to the fetchObject() method # Suppose that you have a pulishers table: and a Publisher class: To fetch each row from the publishers table and returns it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2216","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2216"}],"version-history":[{"count":3,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2216\/revisions"}],"predecessor-version":[{"id":2220,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2216\/revisions\/2220"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}