Home  >  Article  >  Backend Development  >  Which extensions do php need to install to access mysql?

Which extensions do php need to install to access mysql?

PHPz
PHPzOriginal
2023-03-31 09:10:441680browse

PHP is a popular server-side programming language used for developing dynamic web applications. When writing web applications, interacting with the database is an essential part. MySQL is one of the popular databases, and PHP also provides many extensions to interact with MySQL. In this article, we will discuss the extensions required for PHP to connect to MySQL.

  1. MySQLi Extension

MySQLi (MySQL Improved Extension) is an extension for PHP5 and above, used to connect and manage MySQL databases. MySQLi has many advantages, the most important of which is that it supports prepared statements to execute SQL statements safely and conveniently, which can greatly reduce the risk of SQL injection attacks.

To use the MySQLi extension, the MySQLi module needs to be installed on the server. If you are already using an integrated development environment such as WAMP, XAMPP or MAMP, MySQLi may already be installed. Otherwise, you need to download the extension and add it to your PHP installation.

The following is a sample code to connect to a MySQL database using the MySQLi extension:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
  1. PDO extension

PDO (PHP Data Objects) is another interface with MySQL Interactive PHP extension. Similar to MySQLi, PDO can also prepare statements to avoid SQL injection attacks. Additionally, it can be used with other databases such as SQLite, Oracle, IBM, etc.

To use the PDO extension, the PDO library and MySQL driver need to be installed on the server. If you already use an integrated development environment, you may already have these dependencies installed. Otherwise, you need to download them and add them to your PHP installation.

The following is a sample code for connecting to a MySQL database using the PDO extension:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

try {
  // 创建连接
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // 设置 PDO 错误模式为异常
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully"; 
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>
  1. MySQL extension

MySQL extension is the original extension for PHP to connect to the MySQL database. The MySQL extension provides many functions for connecting to and managing MySQL databases. However, since it does not support prepared statements and other modern techniques, its use is no longer recommended.

Although the MySQL extension is considered obsolete, if you must use it, you will need to install the MySQL Client library and the MySQL extension on the server. The following is the sample code to connect to MySQL database using MySQL extensions:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// 创建连接
$conn = mysql_connect($servername, $username, $password);

// 检查连接是否成功
if (!$conn) {
  die("Connection failed: " . mysql_error());
}
echo "Connected successfully";

// 选择数据库
mysql_select_db($dbname, $conn);
?>

Summary

In this article, we discussed the extensions required for PHP to connect to MySQL. MySQLi extension and PDO extension are modern, secure and convenient PHP extensions, which are recommended. If you must use the MySQL extension, make sure you have the MySQL Client library and the MySQL extension installed.

The above is the detailed content of Which extensions do php need to install to access mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn