In this article, I demonstrate How to query a database by id using PHP and MySQL. The database table featured in this tutorial is included below using a GitHub Gist but you can use your own table if you wish.
First set up a database in phpMyAdmin and name it ‘MyShop’ or something similar and run the SQL below to create the products table. Then, set up the PDO connection to the database correctly using your own database name, username and password using the database.php file found in the project download. When you have connected to the database successfully, you should see the products in a list like the demo and product information displayed when each list item is clicked.
The code should output the following view in a browser
Copy and paste the SQL below into phpMyAdmin to create the products table
Here are the project files for How to query a database by id using PHP and MySQL
You can see the project working on this – Live demo
You can download – Query by ID using PHP (90 downloads)