php-mysqlWhat port to connect to MySQL from PHP?
The port to connect to MySQL from PHP is 3306. This port is the default port for MySQL and is used for both TCP and Unix sockets.
Example code to connect to MySQL from PHP:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Output of example code:
Connected successfully
Code explanation
$servername = "localhost";
- This sets the server name to localhost.$username = "username";
- This sets the username for the connection.$password = "password";
- This sets the password for the connection.$conn = new mysqli($servername, $username, $password);
- This creates a new MySQLi connection using the server name, username, and password.if ($conn->connect_error) {
- This checks if there is an error in the connection.die("Connection failed: " . $conn->connect_error);
- This prints an error message if there is an error in the connection.echo "Connected successfully";
- This prints a success message if the connection is successful.
Helpful links
More of Php Mysql
- How to check the result of an insert in PHP and MySQL?
- How to output XML from MySQL using PHP?
- How to create an SSL connection to MySQL using PHP?
- How to use a variable in a MySQL query using PHP?
- How to store a boolean value in a MySQL database using PHP?
- How to generate a UUID in MySQL using PHP?
- How to get the version of MySQL using PHP?
- How to set a timeout for MySQL query in PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to export data from MySQL to Excel using PHP?
See more codes...