Commonly used PHP database connection methods

WBOY
Release: 2023-06-22 15:00:02
Original
3674 people have browsed it

PHP is a widely used server-side scripting language, especially suitable for processing dynamic web content. Connecting to a database is an important skill when it comes to processing and managing data in a database. In this article, we will introduce some commonly used PHP database connection methods, including MySQL, Oracle, PostgreSQL, SQLite, etc.

MySQL database connection method:
MySQL is a widely used relational database management system that is often used in the backend of web applications. The method of connecting to MySQL database in PHP can be achieved by using mysqli or PDO class. Among them, the mysqli class is an extension of PHP 5 and supports both object-oriented and process-oriented programming methods. PDO not only supports MySQL database, but also supports many other databases.

Using mysqli to connect to a MySQL database requires first creating a mysqli object and then using it to execute SQL statements. The specific code is as follows:

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

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

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";
Copy after login

If you want to use PDO to connect to the MySQL database, you can use the following code:

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置PDO错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功"; 
}
catch(PDOException $e)
{
    echo "连接失败: " . $e->getMessage();
}
Copy after login

Oracle database connection method:
Oracle is a commercial relational database management system , supporting large enterprise applications. The method of connecting to the Oracle database in PHP is usually implemented using the oci8 extension. For PHP 7 and above, the oci8 extension is enabled by default. In the following example, we will use the oci_connect() function to connect to the Oracle database.

$dbname = "myDB";
$dbuser = "scott";
$dbpwd = "tiger";
$dbhost = "localhost/XE";

$conn = oci_connect($dbuser, $dbpwd, $dbhost, 'AL32UTF8');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}else{
    echo "连接成功";
}
Copy after login

PostgreSQL database connection method:
PostgreSQL is an advanced open source relational database management system that supports complex data types and advanced SQL statements. The way to connect to a PostgreSQL database in PHP is to use one of the following methods: pg_connect() and the PDO extension. Connecting to PostgreSQL may require more configuration options than MySQL and Oracle.

The sample code for using the pg_connect() function to connect to the PostgreSQL database is as follows:

$dbname = "myDB";
$user = "myuser"; 
$pwd = "mypassword"; 
$host = "localhost";

$conn = pg_connect("host=$host dbname=$dbname user=$user password=$pwd") or die("连接失败");
echo "连接成功";
Copy after login

If you want to use PDO to connect to the PostgreSQL database, you can use the following code:

$dbname = "myDB";
$user = "myuser";
$pwd = "mypassword";
$host = "localhost";

try {
    $conn = new PDO("pgsql:host=$host;dbname=$dbname", $user, $pwd);
    // 设置PDO错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功";
} 
catch(PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}
Copy after login

SQLite database connection Method:
SQLite is a lightweight relational database management system commonly used for local storage. The method of connecting to SQLite database in PHP can use SQLite extension. In the following example, we will use PDO to connect to a SQLite database.

$dbname = 'myDB.sqlite';
$dbpath = __DIR__ . '/database/';

try {
    $conn = new PDO("sqlite:$dbpath.$dbname");
    // 设置PDO错误模式为异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功";
} catch(PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}
Copy after login

Summary:
The above are commonly used database connection methods, which can help you easily connect various types of databases in PHP. No matter which method you use to connect to your database, you should always pay attention to security and reliability. To protect your data from hackers, you should use security measures such as passwords and authorization, and ensure that your code is not vulnerable to vulnerabilities such as SQL injection.

The above is the detailed content of Commonly used PHP database connection methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template