How to Connect to the MySQL Server
Before you can store or get any data, you need to connect to the MySQL database, which might be on the same computer as your PHP scripts or on a different computer. You don’t need to know the details of connecting to the database because PHP handles the details.
All you need to know is the name and location of the database, along with a username and password to access it.
Think of a database connection in the same way that you think of a telephone connection. You don’t need to know the details about how the connection is made — that is, how your words move from your telephone to another telephone — you need to know only the area code and phone number. The phone company handles the details.
To connect to the MySQL server, you need to know the name of the computer on which the database is located and your MySQL account’s user ID and password. For most queries, you also need to know the name of the database with which you want to interact.
To open the connection, use the mysqli_connect function:
$cxn = mysqli_connect("host","acct","password","dbname") or die ("message");
Fill in the following information:
host: The name of the computer on which MySQL is installed — for example, databasehost.example.com. If the MySQL database is on the same computer as your website, you can use localhost as the computer name. If you leave this information blank (“), PHP assumes localhost.
acct: The name of any valid MySQL account.
password: The password for the MySQL account specified by acct. If the MySQL account doesn’t require a password, don’t type anything between the quotes: “.
dbname: The name of the database with which you want to communicate. This parameter is optional — you can select the database later, with a separate command, if you prefer. You can select a different database at any point in your script.
If you’re using the mysql functions, you can’t select the database in the connect function. You must use a separate function — mysql_select_db — to select the database.
message: The message sent to the browser if the connection fails. The connection fails if the computer or network is down, or if the MySQL server isn’t running. It also may fail if the information provided isn’t correct — for example, if the password contains a typo.
You might want to use a descriptive message during development, such as Couldn’t connect to server, but a more general message suitable for customers after you put the application in use, such as The Catalog is not available at the moment. Please try again later.
The host includes a port number that’s needed for the connection. Almost always, the port number is 3306. On rare occasions, the MySQL administrator needs to set up MySQL so that it connects on a different port. In these cases, the port number is required for the connection. The port number is specified as hostname:portnumber. For instance, you might use localhost:8808.
With these statements, mysqli_connect attempts to open a connection to the named computer, using the account name and password provided. If the connection fails, the script stops running and sends message to the browser.
The following statement connects to the MySQL server on the local computer, using a MySQL account named phpuser that doesn’t require a password:
$cxn = mysqli_connect("localhost","phpuser",","Customer") or die ("Couldn't connect to server.");
For security reasons, you should store the connection information in variables and use the variables in the connection statement, as follows:
$host="localhost"; $user="phpuser"; $password="; $dbname = "Customer"; $cxn = mysqli_connect($host,$user,$password,$dbname) or die("Couldn't connect to server.");
For even more security, you can put the assignment statements for the connection information in a separate file in a hidden location so that the account name and password aren’t even in the script. You insert the account information from the file by using an include statement.
The variable $cxn contains information that identifies the connection. You can have more than one connection open at a time by using more than one variable name.
A connection remains open until you close it or until the script ends. You close a connection as follows:
For instance, to close the connection in the preceding example, use this statement: