5.3. Communicating with MySQL
This chapter describes accessing MySQL from PHP scripts. (Accessing MySQL databases outside of PHP scripts is discussed in Chapters 1-4 in this minibook.) SQL queries are sent to MySQL using PHP functions. Communicating with MySQL involves the following steps:
Connect to the MySQL server.
Send the SQL query.
5.3.1. Connecting to the MySQL server
Before you can store or get any data, you need to connect to the 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. 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 ...
Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.