The function’s syntax is as shown:
It returns a Boolean true on success and Boolean false on fail. You can then use the return values to perform actions using if…else statements.
The following example shows you how to use the mysql_connect function to establish a primary connection to the MySQL database.
The example below uses hostname:port to connect to a MySQL server.
You can also use socket connection as shown in the example below.
MySQLi or PDO
In recent versions of PHP, you will notice the MySQL extension is deprecated and no longer supported.
To use MySQL and PHP with no errors, use the MySQLi extension or PHP Data Objects.
We can not go over the details and how either PDO or MySQLi works. However, MySQLi will only work with the MySQL database. On the other hand, PDO will work on different database systems, which can be beneficial if you need to switch.
By default, the PHP MySQLi extension is installed and enabled on most systems. You can learn how to install PDO extension in the resource provided below:
This short guide gives a walkthrough of using the mysql_connect function to create connections to the database.
Thanks for reading.