{"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
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\nThe 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\nNow 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\nNext, 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\necho 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\nWe’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\nNow, 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