{"id":3458,"date":"2019-11-14T15:37:01","date_gmt":"2019-11-14T20:37:01","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=3458"},"modified":"2019-11-14T15:37:02","modified_gmt":"2019-11-14T20:37:02","slug":"reading-from-a-mysql-database-using-php-and-mysqli","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli","title":{"rendered":"Reading from a MySQL Database Using PHP and MySQLi"},"content":{"rendered":"\n

I had some old tutorials out there that use outdated PHP libraries to access a MySQL database. I wanted to update these to use the latest PHP library called MySQLi. MySQLi is the improved database driver for use in the PHP scripting language to provide an interface to MySQL databases. It replaces both PHP’s MySQL extensions and PHP Data Objects (PDO).<\/p>\n\n\n\n

Connecting to a MySQL Database Using PHP<\/h2>\n\n\n\n

This tutorial is going to assume you have a web server capable of running PHP and a test MySQL database available. First, we need to connect to the database using PHP.<\/p>\n\n\n

\n<?\n$mysqli = new mysqli("localhost", "root", "root", "local");\n?>\n<\/pre><\/div>\n\n\n

The code above first creates a connection to your database. The order of the parameters is server\/host, user, password, database name. <\/p>\n\n\n\n

Next, we’ll add some code to validate our connect. If there is an error we will output it. Otherwise, we’ll output the host information so we know we have a connection.<\/p>\n\n\n

\n<?\n$mysqli = new mysqli("localhost", "root", "root", "local");\n\nif ($mysqli->connect_errno) {\n    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;\n}\n\necho $mysqli->host_info . "\\n";\n?>\n<\/pre><\/div>\n\n\n

Now when I execute this code I see the following in my browser:<\/p>\n\n\n\n

Localhost via UNIX socket<\/pre>\n\n\n\n

Next, we’ll add our SQL query. I have a test WordPress database and I want to return the usernames of all users. To do that in my dastabase I want to run the query “Select user_login from wp_users”. I will store the results into a variable called results<\/strong>. If an error occurs I will output the error statement.<\/p>\n\n\n

\n<?\n$mysqli = new mysqli("localhost", "root", "root", "local");\n\nif ($mysqli->connect_errno) {\n    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;\n}\n\nif (!$results = $mysqli->query("SELECT user_login FROM WP_USERS")){\n    echo "Select statement failed: (" . $mysqli->errno . ") " . $mysqli->error;\n}\n\necho var_dump($results->fetch_all());\n?>\n<\/pre><\/div>\n\n\n

echo var_dump($results->fetch_all());<\/strong> Is going to print the results array to our screen. We can take that array and output it as an HTML list pretty easily.<\/p>\n\n\n\n

We’re going to replace fetch_all() with fetch_assoc. This gives us an associative array where the key is the column name and the value is the column value. This allows us to select the username column that is called user_login<\/strong>.<\/p>\n\n\n

\n<?\n$mysqli = new mysqli("localhost", "root", "root", "local");\n\nif ($mysqli->connect_errno) {\n    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;\n}\n\nif (!$results = $mysqli->query("SELECT user_login FROM WP_USERS")){\n    echo "Select statement failed: (" . $mysqli->errno . ") " . $mysqli->error;\n}\n?>\n\n<ul>\n<? while ($row = $results->fetch_assoc()) { ?>\n  <li><?= $row['user_login']; ?><\/li>\n<? } ?>\n<\/ul>\n<\/pre><\/div>\n\n\n

Now, what you see is we create a while loop where $row = $results->fetch_assoc(). This gives us a $row variable to use in the loop that has the key we want. So inside the loop we just output the value using the ?= shortcut. <\/p>\n\n\n\n

Our results look something like this. Showing the two users of our WordPress application.<\/p>\n\n\n\n

\"the<\/figure>\n\n\n\n

Hopefully, you found this tutorial helpful. Please leave a comment if you have any questions. Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"

I had some old tutorials out there that use outdated PHP libraries to access a MySQL database. I wanted to update these to use the latest PHP library called MySQLi. MySQLi is the improved database driver for use in the PHP scripting language to provide an interface to MySQL databases. It replaces both PHP’s MySQL … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":3459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"yoast_head":"\nReading from a MySQL Database Using PHP and MySQLi<\/title>\n<meta name=\"description\" content=\"I had some old tutorials out there that use outdated PHP libraries to access a MySQL database. I wanted to update these to use the latest PHP library\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Johnathan Ward\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/mysql_logo.png\",\"contentUrl\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/mysql_logo.png\",\"width\":1024,\"height\":512,\"caption\":\"mysqli php database tutorial\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli\",\"name\":\"Reading from a MySQL Database Using PHP and MySQLi\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#primaryimage\"},\"datePublished\":\"2019-11-14T20:37:01+00:00\",\"dateModified\":\"2019-11-14T20:37:02+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\"},\"description\":\"I had some old tutorials out there that use outdated PHP libraries to access a MySQL database. I wanted to update these to use the latest PHP library\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Reading from a MySQL Database Using PHP and MySQLi\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\",\"name\":\"Johnathan Ward\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"caption\":\"Johnathan Ward\"},\"description\":\"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.\",\"sameAs\":[\"http:\/\/johnathanward.com\",\"https:\/\/twitter.com\/spyderman4g63\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reading from a MySQL Database Using PHP and MySQLi","description":"I had some old tutorials out there that use outdated PHP libraries to access a MySQL database. I wanted to update these to use the latest PHP library","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:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli","twitter_misc":{"Written by":"Johnathan Ward","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#primaryimage","inLanguage":"en-US","url":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/mysql_logo.png","contentUrl":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2019\/11\/mysql_logo.png","width":1024,"height":512,"caption":"mysqli php database tutorial"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli","name":"Reading from a MySQL Database Using PHP and MySQLi","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#primaryimage"},"datePublished":"2019-11-14T20:37:01+00:00","dateModified":"2019-11-14T20:37:02+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9"},"description":"I had some old tutorials out there that use outdated PHP libraries to access a MySQL database. I wanted to update these to use the latest PHP library","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/php-tutorials\/reading-from-a-mysql-database-using-php-and-mysqli#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Reading from a MySQL Database Using PHP and MySQLi"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9","name":"Johnathan Ward","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","caption":"Johnathan Ward"},"description":"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.","sameAs":["http:\/\/johnathanward.com","https:\/\/twitter.com\/spyderman4g63"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/3458"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=3458"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/3458\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media\/3459"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=3458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=3458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=3458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}