Home > Article > Backend Development > How to set the port number of MySQL in PDO
When using PHP to connect to the MySQL database, many people will use PDO (PHP Data Object) to connect. PDO is an extension library of PHP. It provides a unified data access interface that can connect to multiple database types, including MySQL, Oracle, PostgreSQL, etc. However, sometimes we need to set the port number used to connect to the MySQL database. This article will introduce how to set the MySQL port number in PDO.
Generally, the port number for connecting to the MySQL database is 3306 by default. If your MySQL database has not changed the port number, then you can directly use PDO to connect without setting the port number. But if your MySQL database changes the port number, or you need to use other port numbers to connect to the MySQL database, then you need to set it through PDO.
PHP's PDO class provides a constructor that can be used to set the port number to connect to the MySQL database. The following is a basic code example for using PDO to connect to a MySQL database and set the port number:
$dsn = 'mysql:host=127.0.0.1;port=3307;dbname=test'; $username = 'root'; $password = '123456'; try { $pdo = new PDO($dsn, $username, $password); echo '连接成功'; } catch (PDOException $e) { echo '连接失败:' . $e->getMessage(); }
In the above code, we first define a dsn string, which contains the address and port number of the MySQL database to be connected. and database name. Here we set the port to 3307 and change it to the corresponding port number of our MySQL server. Then we defined the username and password required to connect to the MySQL database. Finally, in the try block, we use the constructor of PDO to try to connect to the MySQL database, and output a prompt of success or failure of the connection through the echo statement.
In addition to explicitly specifying the port number in the dsn string, you can also specify the port number by setting the PDO::MYSQL_ATTR_PORT constant in the options parameter of the constructor. The following is a code example that uses the options parameter to set the port number:
$dsn = 'mysql:host=127.0.0.1;dbname=test'; $username = 'root'; $password = '123456'; $options = array(PDO::MYSQL_ATTR_PORT => 3307); try { $pdo = new PDO($dsn, $username, $password, $options); echo '连接成功'; } catch (PDOException $e) { echo '连接失败:' . $e->getMessage(); }
In the above code, we first define a dsn string containing the address and database name of the MySQL database to be connected, without explicitly specifying the port. Number. Then define the username and password required to connect to the MySQL database. Finally, the PDO::MYSQL_ATTR_PORT constant is set in the $options array and its value is assigned to 3307, which is the port number. Finally, in the try block, we use the constructor of PDO to try to connect to the MySQL database, and output a prompt of success or failure of the connection through the echo statement.
So far, you have learned how to use PDO to connect to the MySQL database and set the port number. I hope it will be helpful to you.
The above is the detailed content of How to set the port number of MySQL in PDO. For more information, please follow other related articles on the PHP Chinese website!