Wednesday, 8 August 2018

PHP: Connecting To A MySQL Database.

This is a simple tutorial on how to connect to a MySQL database with PHP. Here, I hope to provide a simple explanation about the functions that are used and the connection details that are needed.
There are three main parameters that you need to understand:
  • Server / Host: This is the location of the database server (sometimes referred to as the host). You can think of this as a URL of sorts. On your local machine, it will probably be “localhost”. On your web host, it could be something like “mysql38.example.com”.
  • User: The username of the MySQL user that you want to connect as.
  • Password: The password of the MySQL user that you want to connect as.
When connecting to a MySQL server, you will need to provide these details!

mysql_connect

Although the mysql_* functions are deprecated, I felt as though I couldn’t exclude them from this tutorial. Using the function mysql_connect, we can connect to MySQL like so:
Note that you will obviously need to use the connection details that are relevant to your database server! In many cases, a local MySQL installation will have the following default details:
  • Server: localhost
  • User: root
  • Password: *blank* (No seriously – Most default MySQL installations have a blank password by default. In these cases, we have to supply an empty string. If we attempt to supply a password when there is no password, we will receive the following error: “Access denied for user ‘root’@’localhost’ (using password: YES)”).
If you are using a default account on a local installation, then your connection code will probably look like this:
If a connection fails, the mysql_connect function will return a boolean FALSE value. If the connection is successful, then it will return a MySQL connection resource that can be used to query the database.

Selecting the database.

Before you can run any SQL queries on your tables, you will need to do two things:
  1. Connect to MySQL (already covered above).
  2. Select a database.
Remember that you can have multiple databases on the same MySQL server. Hence the reason that we must select a database before attempting to run any SQL queries on our tables! An example of connecting to MySQL and selecting a database with PHP:
Using the function mysql_select_db, we were able to select our database AFTER connecting to MySQL. The process will always be the same:
  1. Connect to the server.
  2. Select your database.
  3. Query your tables.
A lot of beginners struggle to grasp this, simply because many tutorials toss them straight into the deep end without explaining the basic steps that need to be taken.

PDO.

As I mentioned above, the mysql_* functions are deprecated. This means that you should NOTuse them to build new web applications! Instead, you should use something like the PDO object:
If you look closely, you’ll see that the same parameters are being used. We have our server (aka, the host), which is ‘localhost’. We have our username, which is ‘root’. And we have our password, which is ‘super_secret_password’. Note how you can also supply a parameter called dbname (in this example, the database name is ‘our_db’)! This means that we can connect to our MySQL server and select our database in one line of code! Handy, right?
Let’s connect using the default MySQL details:
Hopefully, separating the details onto different lines has made it a bit clearer! Feel free to use the code above; inserting your own details where needed.

PHP won’t connect to MySQL.

This can be a frustrating issue. Thankfully, there are ways to debug our connection issues. The first order of business is to check for connection errors. How you catch these errors depends on what library / functions you are using:

PDO connection error.

Detecting connection errors w/ PDO is pretty straight-forward, as the library allows you to throw exceptions:
If you fail to connect for whatever reason, the code snippet above will throw an exception error. Some common examples:
Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ‘
This occurs when you have entered an incorrect host name / server name.
Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [1045] Access denied for user ‘root’@’localhost’ (using password: YES)’
This occurs when you use the wrong password (or you enter a password, even though the account in question doesn’t have one).
Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [1049] Unknown database ‘blah”
This one is pretty straightforward. Basically, the database “blah” does not exist!

mysql_connect connection error.

Here’s an example of error-checking with the mysql_* functions:
If you purposely use incorrect details (for testing purposes), you’ll see that the errors are relatively the same as the ones that are thrown while using the PDO object.

0 comments:

Post a Comment