{"id":1137,"date":"2012-02-07T19:18:54","date_gmt":"2012-02-07T19:18:54","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2012\/02\/07\/grab-all-comments-from-database\/"},"modified":"2023-08-15T18:11:07","modified_gmt":"2023-08-15T22:11:07","slug":"grab-all-comments-from-database","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/","title":{"rendered":"How to use PHP to Connect and Retrieve Data from MySQL"},"content":{"rendered":"<p>In our <a href=\"https:\/\/www.inmotionhosting.com\/support\/website\/dynamic-website-vs-static-website\/\">previous set of articles<\/a>, we\u2019ve created a simple 2 page website that allows users to submit comments about the page they were looking at. In this article, we\u2019re going to show you how to use PHP to <strong>Connect<\/strong> to and <strong>Retrieve Data<\/strong> from <strong>MySQL<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1. Create our SQL Query to grab all comments<\/h2>\n\n\n\n<p>In order to display comments on a page, we first need to know what comments to show. When we set up our site we created two pages, and each page was assigned a unique id number. This ID number will be used to gather comments for that specific page. For example, when the user is on page 1, we\u2019ll select all of the comments in the database assigned to page \u201c1\u201d. To ensure seamless functionality like this, it\u2019s essential to have reliable <strong><a href=\"https:\/\/www.inmotionhosting.com\/php-hosting\">PHP hosting<\/a><\/strong> that supports efficient database interactions.<\/p>\n\n\n\n<p>If you\u2019re not familiar with SQL, you can use phpMyAdmin to help write your SQL command. To do this:<\/p>\n\n\n\n<ol class=\"article_list wp-block-list\">\n<li><a href=\"\/support\/edu\/cpanel\/how-to-log-into-cpanel\/\">Log into cPanel<\/a> and click the phpMyAdmin icon<\/li>\n\n\n\n<li>In the left menu, first click your database name and then click the table to work with. If you\u2019re following our example, we\u2019ll first click on \u201c_mysite\u201d and then \u201ccomments\u201d.<\/li>\n\n\n\n<li>Click \u201cSearch\u201d in the top menu<\/li>\n\n\n\n<li>Enter 1 for the \u201cValue\u201d of \u201carticleid\u201d and then click \u201cGo\u201d<br><figure><a href=\"\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif\" rel=\"lightbox-0\"><img decoding=\"async\" width=\"1024\" height=\"448\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search-1024x448.gif\" class=\"optimized-lcp-image\" alt=\"create-sample-select-command-using-phpmyadmin-use-search\" loading=\"eager\" fetchpriority=\"high\" sizes=\"(max-width: 768px) 100vw, 768px\" srcset=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search-1024x448.gif 1024w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search-300x131.gif 300w, https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search-768x336.gif 768w\"><\/a><\/figure>\n<div style=\"clear: both;\">\u00a0<\/div>\n<\/li>\n\n\n\n<li>After running the search, phpMyAdmin will show you all comments that belong to article 1, as well as the SQL syntax you can use to select those comments. The code provided is:\n<code>SELECT * FROM `comments` WHERE `articleid` =1 LIMIT 0 , 30<\/code>\n<figure><a href=\"\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_our-sample-select-query-from-phpmyadmin.gif\" rel=\"lightbox-0\"><img loading=\"lazy\" decoding=\"async\" class=\"std_ss size-full wp-image-6897\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_our-sample-select-query-from-phpmyadmin.gif\" alt=\"our-sample-select-query-from-phpmyadmin\" width=\"959\" height=\"448\"><\/a><\/figure><div style=\"margin-top: 5px;\"><figure><\/figure>\n<p>\u00a0<\/p>\n<div style=\"clear: both;\">\u00a0<\/div>\n<\/div>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2. Setting up our PHP code to SELECT our comments<\/h2>\n\n\n\n<p class=\"alert alert-danger\">Note that <strong>mysqli_fetch_array<\/strong> was deprecated in PHP versions below 7.0. As of 7.0, the code has been removed and replaced with <b>mysqli_fetch-array<\/b>.<\/p>\n\n\n\n<p>Now that we have our sample SQL query, we can use it to create the php code that will print all comments on a page. Below is the example code that we created. If you\u2019re not familiar with php, any line that begins with a \/\/ is a comment, and comments are used by developers to document their code. In our example, we have quite a few comments to help explain what the code is doing, but keep in mind that most scripts do not have as many comments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?\n\n\/\/ At this point in the code, we want to show all of the comments\n\/\/ submitted by users for this particular page. As the comments\n\/\/ are stored in the database, we will begin by connecting to\n\/\/ the database\n \n\/\/ Below we are setting up our connection to the server. Because\n\/\/ the database lives on the same physical server as our php code,\n\/\/ we are connecting to \"localhost\". inmoti6_myuser and mypassword\n\/\/ are the username and password we setup for our database when\n\/\/ using the \"MySQL Database Wizard\" within cPanel\n\n$con = mysql_connect(\"localhost\",\"inmoti6_myuser\",\"mypassword\");\n \n\/\/ The statement above has just tried to connect to the database.\n\/\/ If the connection failed for any reason (such as wrong username\n\/\/ and or password, we will print the error below and stop execution\n\/\/ of the rest of this php script\n\nif (!$con)\n{\n  die('Could not connect: ' . mysql_error());\n}\n \n\/\/ We now need to select the particular database that we are working with\n\/\/ In this example, we setup (using the MySQL Database Wizard in cPanel) a\n\/\/ database named inmoti6_mysite\n\nmysql_select_db(\"inmoti6_mysite\", $con);\n\n\/\/ We now need to setup our SQL query to grab all comments from this page.\n\/\/ The example SQL query we copied from phpMyAdmin is:\n\/\/ SELECT * FROM `comments` WHERE `articleid` =1 LIMIT 0 , 30\n\/\/ If we run this query, it will ALWAYS grab only the comments from our\n\/\/ article with an id of 1. We therefore need to update the SQL query\n\/\/ so that on article 2 is searches for the \"2\", on page is searches for\n\/\/ \"3\", and so on.\n\/\/ If you notice in the URL, the id of the article is set after id=\n\/\/ For example, in the following URL:\n\/\/ https:\/\/phpandmysql.inmotiontesting.com\/page2.php?id=2\n\/\/ ... the article id is 2. We can grab and store this number in a variable\n\/\/ by using the following code:\n\n$article_id = $_GET['id'];\n\n\/\/ We also want to add a bit of security here. We assume that the $article_id\n\/\/ is a number, but if someone changes the URL, as in this manner:\n\/\/ https:\/\/phpandmysql.inmotiontesting.com\/page2.php?id=malicious_code_goes_here\n\/\/ ... then they will have the potential to run any code they want in your\n\/\/ database. The following code will check to ensure that $article_id is a number.\n\/\/ If it is not a number (IE someone is trying to hack your website), it will tell\n\/\/ the script to stop executing the page\n\nif( ! is_numeric($article_id) )\n  die('invalid article id');\n\n\/\/ Now that we have our article id, we need to update our SQL query. This\n\/\/ is what it looks like after we update the article number and assign the\n\/\/ query to a variable named $query\n\n$query = \"SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30\";\n\n\/\/ Now that we have our Query, we will run the query against the database\n\/\/ and actually grab all of our comments\n\n$comments = mysql_query($query);\n\n\/\/ Before we start writing all of the comments to the screen, let's first\n\/\/ print a message to the screen telling our users we're going to start\n\/\/ printing comments to the page.\n\necho \"&lt;h1&gt;User Comments&lt;\/h1&gt;\";\n\n\/\/ We are now ready to print our comments! Below we will loop through our\n\/\/ comments and print them one by one.\n\n\/\/ The while statement will begin the \"looping\"\n\n\/*NOTE that in PHP 7.0, the mysql_fetch_array has been removed -it was previously deprecated \nin earlier versions of PHP.  You find the cod documentation here:  \nhttps:\/\/php.net\/manual\/en\/function.mysql-fetch-array.php *\/\n\nwhile($row = mysql_fetch_array($comments, MYSQL_ASSOC))\n{\n\n  \/\/ As we loop through each comment, the specific comment we're working\n  \/\/ with right now is stored in the $row variable.\n\n  \/\/ for example, to print the commenter's name, we would use:\n  \/\/ $row['name']\n  \n  \/\/ if we want to print the user's comment, we would use:\n  \/\/ $row['comment']\n  \n  \/\/ As this is a beginner tutorial, to make our code easier to read\n  \/\/ we will take the values above (from our array) and put them into\n  \/\/ individual variables\n\n  $name = $row['name'];\n  $email = $row['email'];\n  $website = $row['website'];\n  $comment = $row['comment'];\n  $timestamp = $row['timestamp'];\n\n  $name = htmlspecialchars($row['name'],ENT_QUOTES);\n  $email = htmlspecialchars($row['email'],ENT_QUOTES);\n  $website = htmlspecialchars($row['website'],ENT_QUOTES);\n  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);\n  \n  \/\/ We will now print the comment to the screen\n  \n  echo \"  &lt;div style='margin:30px 0px;'&gt;\n      Name: $name&lt;br \/&gt;\n      Email: $email&lt;br \/&gt;\n      Website: $website&lt;br \/&gt;\n      Comment: $comment&lt;br \/&gt;\n      Timestamp: $timestamp\n    &lt;\/div&gt;\n  \";\n}\n\n\/\/ At this point, we've added the user's comment to the database, and we can\n\/\/ now close our connection to the database:\nmysql_close($con);\n\n?&gt;<\/code><\/pre>\n\n\n\n<p>As stated earlier, we purposely include many comments to help explain what the code was doing. While the example code above looks like a lot of work, if we strip out all of the comments, the code looks more like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?\n\n$con = mysql_connect(\"localhost\",\"inmoti6_myuser\",\"mypassword\");\n \nif (!$con)\n{\n  die('Could not connect: ' . mysql_error());\n}\n \nmysql_select_db(\"inmoti6_mysite\", $con);\n\n$article_id = $_GET['id'];\n\nif( ! is_numeric($article_id) )\n  die('invalid article id');\n\n$query = \"SELECT * FROM `comments` WHERE `articleid` =$article_id LIMIT 0 , 30\";\n\n$comments = mysql_query($query);\n\necho \"&lt;h1&gt;User Comments&lt;\/h1&gt;\";\n\n\/\/ Please remember that  mysql_fetch_array has been deprecated in earlier\n\/\/ versions of PHP.  As of PHP 7.0, it has been replaced with mysqli_fetch_array.  \n\nwhile($row = mysql_fetch_array($comments, MYSQL_ASSOC))\n{\n  $name = $row['name'];\n  $email = $row['email'];\n  $website = $row['website'];\n  $comment = $row['comment'];\n  $timestamp = $row['timestamp'];\n  \n  \/\/ Be sure to take security precautions! Even though we asked the user\n  \/\/ for their \"name\", they could have typed anything. A hacker could have\n  \/\/ entered the following (or some variation) as their name:\n  \/\/\n  \/\/ &lt;script type=\"text\/javascript\"&gt;window.location = \"https:\/\/SomeBadWebsite.com\";&lt;\/script&gt;\n  \/\/\n  \/\/ If instead of printing their name, \"John Smith\", we would be printing\n  \/\/ javascript code that redirects users to a malicious website! To prevent\n  \/\/ this from happening, we can use the &lt;a href=\"https:\/\/php.net\/htmlspecialchars\" target=\"_blank\"&gt;htmlspecialchars function&lt;\/a&gt; to convert\n  \/\/ special characters to their HTML entities. In the above example, it would\n  \/\/ instead print:\n  \/\/\n  \/\/ &lt;span style=\"color:red;\"&gt;&lt;&lt;\/span&gt;script type=&lt;span style=\"color:red;\"&gt;\"&lt;\/span&gt;text\/javascript&lt;span style=\"color:red;\"&gt;\"&gt;&lt;\/span&gt;window.location = &lt;span style=\"color:red;\"&gt;\"&lt;\/span&gt;https:\/\/SomeBadWebsite.com&lt;span style=\"color:red;\"&gt;\"&lt;\/span&gt;;&lt;span style=\"color:red;\"&gt;&lt;&lt;\/span&gt;\/script&lt;span style=\"color:red;\"&gt;&gt;&lt;\/span&gt;\n  \/\/\n  \/\/ This certainly would look strange on the page, but it would not be harmful\n  \/\/ to visitors\n  \n  $name = htmlspecialchars($row['name'],ENT_QUOTES);\n  $email = htmlspecialchars($row['email'],ENT_QUOTES);\n  $website = htmlspecialchars($row['website'],ENT_QUOTES);\n  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);\n  \n  echo \"  &lt;div style='margin:30px 0px;'&gt;\n      Name: $name&lt;br \/&gt;\n      Email: $email&lt;br \/&gt;\n      Website: $website&lt;br \/&gt;\n      Comment: $comment&lt;br \/&gt;\n      Timestamp: $timestamp\n    &lt;\/div&gt;\n  \";\n}\n\nmysql_close($con);\n\n?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3. Placing our php code into our pages<\/h2>\n\n\n\n<p>We now have our php code that will display comments to the screen. In a previous article, we explained <a href=\"\/support\/website\/using-php-include-function\/\">how to use php\u2019s include function to reuse code<\/a>, and we will continue to use this method to use our php code.<\/p>\n\n\n\n<p><strong>To incorporate our php code:<\/strong><\/p>\n\n\n\n<ol class=\"article_list\"><li>Create a file named display_comments.php<\/li><li>Paste in the sample code above<\/li><li>Update both page1.php and page2.php to include display_comments.php by using:\n<code>&lt;? include(\"display_comments.php\"); ?&gt;<\/code>\n<p>towards the bottom of the page where you want to display comments.<\/p>\n<\/li><\/ol>\n\n\n\n<p>After performing the steps above, our page1.php file now looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;? include(\"manage_comments.php\"); ?&gt;\n\n&lt;h1&gt;This is page1.php&lt;\/h1&gt;\n\n&lt;div&gt;&lt;a href='page2.php?id=2'&gt;Click here&lt;\/a&gt; to go to page2.php&lt;\/div&gt;\n\n&lt;div style='margin:20px; width:100px; height:100px; background:blue;'&gt;&lt;\/div&gt;\n\n&lt;? include(\"display_comments.php\"); ?&gt;\n\n&lt;? include(\"formcode.php\"); ?&gt;<\/code><\/pre>\n\n\n\n<p>After testing our two pages, you can see that each page shows only the comments that were added to that particular page:<\/p>\n\n\n\n<table border=\"1\" cellpadding=\"3\" cellspacing=\"3\">\n<tbody>\n<tr>\n<td>\n  <p><b>https:\/\/phpandmysql.inmotiontesting.com\/page1.php?id=1<\/b><\/p>\n<\/td>\n<td bgcolor=\"#D3D3D3\">\n  <p><b>https:\/\/phpandmysql.inmotiontesting.com\/page2.php?id=2<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><a href=\"\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_page1.php-with-comments.gif\"><img decoding=\"async\" class=\"std_ss\" alt=\"page1.php-with-comments\" src=\"https:\/\/inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_page1.php-with-comments.gif\" style=\"max-width: 250px;\"><\/a><\/td>\n<td bgcolor=\"#D3D3D3\"><a href=\"\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_page2.php-with-comments.gif\"><img decoding=\"async\" class=\"std_ss\" alt=\"page2.php-with-comments\" src=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_page2.php-with-comments.gif\" style=\"max-width: 250px;\"><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In our previous set of articles, we\u2019ve created a simple 2 page website that allows users to submit comments about the page they were looking at. In this article, we\u2019re going to show you how to use PHP to Connect to and Retrieve Data from MySQL. Step 1. Create our SQL Query to grab all<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\"> Read More ><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4288,4305],"tags":[],"class_list":["post-1137","post","type-post","status-publish","format-standard","hentry","category-website","category-databases"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to use PHP to Connect and Retrieve Data from MySQL | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"In this lesson, we will continue building our fictitious website by using PHP to select all user comments from a database and display them in our article.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use PHP to Connect and Retrieve Data from MySQL | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"In this lesson, we will continue building our fictitious website by using PHP to select all user comments from a database and display them in our article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-07T19:18:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-15T22:11:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif\" \/>\n<meta name=\"author\" content=\"Brad Markle\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brad Markle\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\"},\"author\":{\"name\":\"Brad Markle\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf\"},\"headline\":\"How to use PHP to Connect and Retrieve Data from MySQL\",\"datePublished\":\"2012-02-07T19:18:54+00:00\",\"dateModified\":\"2023-08-15T22:11:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\"},\"wordCount\":571,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif\",\"articleSection\":[\"Website\",\"Working with Databases\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\",\"name\":\"How to use PHP to Connect and Retrieve Data from MySQL | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif\",\"datePublished\":\"2012-02-07T19:18:54+00:00\",\"dateModified\":\"2023-08-15T22:11:07+00:00\",\"description\":\"In this lesson, we will continue building our fictitious website by using PHP to select all user comments from a database and display them in our article.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif\",\"width\":1081,\"height\":473,\"caption\":\"create-sample-select-command-using-phpmyadmin-use-search\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use PHP to Connect and Retrieve Data from MySQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf\",\"name\":\"Brad Markle\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/bradm\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use PHP to Connect and Retrieve Data from MySQL | InMotion Hosting","description":"In this lesson, we will continue building our fictitious website by using PHP to select all user comments from a database and display them in our article.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/","og_locale":"en_US","og_type":"article","og_title":"How to use PHP to Connect and Retrieve Data from MySQL | InMotion Hosting","og_description":"In this lesson, we will continue building our fictitious website by using PHP to select all user comments from a database and display them in our article.","og_url":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2012-02-07T19:18:54+00:00","article_modified_time":"2023-08-15T22:11:07+00:00","og_image":[{"url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif","type":"","width":"","height":""}],"author":"Brad Markle","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Brad Markle","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/"},"author":{"name":"Brad Markle","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf"},"headline":"How to use PHP to Connect and Retrieve Data from MySQL","datePublished":"2012-02-07T19:18:54+00:00","dateModified":"2023-08-15T22:11:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/"},"wordCount":571,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage"},"thumbnailUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif","articleSection":["Website","Working with Databases"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/","url":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/","name":"How to use PHP to Connect and Retrieve Data from MySQL | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage"},"thumbnailUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif","datePublished":"2012-02-07T19:18:54+00:00","dateModified":"2023-08-15T22:11:07+00:00","description":"In this lesson, we will continue building our fictitious website by using PHP to select all user comments from a database and display them in our article.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#primaryimage","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2012\/02\/website_phpmysql_display_comments_create-sample-select-command-using-phpmyadmin-use-search.gif","width":1081,"height":473,"caption":"create-sample-select-command-using-phpmyadmin-use-search"},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/grab-all-comments-from-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to use PHP to Connect and Retrieve Data from MySQL"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/5ae05d1210b0ef63c437ccedce2799bf","name":"Brad Markle","url":"https:\/\/www.inmotionhosting.com\/support\/author\/bradm\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/1137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=1137"}],"version-history":[{"count":12,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/1137\/revisions"}],"predecessor-version":[{"id":106043,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/1137\/revisions\/106043"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=1137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=1137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=1137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}