Connecting to MySQL using PHP

Last updated: January 12, 2023
Audience: All UW

MySQL Scripting With PHP

Connecting and interacting with a MySQL database using PHP or any other language is a fairly complex topic. This code example provides a simple way to test your connection and provides one particular way to structure your MySQLi object in order to properly connect to your MySQL server.

$mysqli_connection = new MySQLi('your_host.u.washington.edu', 'root', 'your_password', 'your_db', your_port);
if($mysqli_connection->connect_error){
   echo "Not connected, error: ".$mysqli_connection->connect_error;
}
else{
   echo "Connected.";
}

Resources