Author: Waqar A
Updated on: 2021-04-20
Posted on: 2021-04-20
Viewers: 780 (April 2021)
Package: PHP MySQL Database
There are many classes to connect to a database. The best and the ones that are simple and easy to understand, so everybody can use them with a good sense of what is going on in the class.
Read this short tutorial article to learn how you can use the PHP MySQL Database class to perform all database query functions with the least complexity.
In this article you will learn:
What is the PHP MySQL Database Class
How to Configure the Database Access to Use This Class
How to Query a Database
How to Setup a Database Connection
How to Query a Database
Sample Data to Test the Class
How to Download the Update Site Package or Install it With PHP Composer
What is the PHP MySQL Database Class
Based on KISS principle (Keep it simple and stupid/short) the class is simple, short and easy to understand. Unlike complex classes where often developers do not understand working of class and end up just using it without understanding.his class however is simple enough for users to understand what they are doing.
How to Configure the Database Access to Use This Class
The database configuration, which includes database username, password etc is kept in separate file for better security.
Edit config.ini.php to set your database configuration values. For better security, keep this file out of Web document root directory, which in some cases may be public/www . If you change the location of the configuration file, update the path of the file in the database.class.php . By default all files are kept in same directory.
$this->db = parse_ini_file("config.ini.php");
How to Query a Database
How to Setup a Database Connection
Make sure you have edited configuration file with your database details first, then create a new instance of database class.
$db = new MySQLDb;
How to Query a Database
Once new instance of the class is successfully created, it will establish a connection to database. You are now ready to run a SQL query through query method.
$query = "select * from guestbook";
$result = $db->query($query);
To fetch the result set into an array, use fetch() method after running query:
$rows = $db->fetch(); var_dump($rows);
All rows would be returned into $rows array which can be used with any loop to echo.
To get count of returned rows, use $result object's property:
$result->num_rows
Do a var_dump() to see details of returned result object, in case of successful insert, update and delete query $result object returns true, this can be evaluated with simple if/else conditional.
echo ($result) ? "query successful" : "query was not successful";
Select Query
Insert Query
Update Query
Delete Query
Sample Data to Test the Class
CREATE TABLE `guestbook` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user` varchar(20) CHARACTER SET utf8 NOT NULL DEFAULT '',
`message` varchar(400) CHARACTER SET utf8 NOT NULL DEFAULT '',
`date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
insert into guestbook (user, message, DATE) values ('me', 'anything', now());
insert into guestbook (user, message, DATE) values ('user', 'a message', now());
insert into guestbook (user, message, DATE) values ('user1', 'new message', now());
How to Download the Update Site Package or Install it With PHP Composer
The PHP Database Class package is available for you to download as a ZIP archive by going to the download page or install it using the PHP Composer Tool by going to the installation instructions page.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
No comments were submitted yet.