温馨提示×

PHP函数如何操作数据库

PHP
小樊
81
2024-11-06 13:24:01
栏目: 编程语言

在PHP中,操作数据库通常使用数据库扩展(如MySQLi、PDO等)。这里我将向您展示如何使用MySQLi和PDO来操作数据库。

1. 使用MySQLi操作数据库:

首先,确保已经安装了MySQLi扩展。然后,按照以下步骤操作数据库:

1.1 创建数据库连接

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

1.2 执行SQL查询

$sql = "SELECT * FROM your_table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 结果";
}

1.3 插入数据

$sql = "INSERT INTO your_table (name, email) VALUES ('John Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

1.4 更新数据

$sql = "UPDATE your_table SET name='Jane Doe' WHERE id=1";
if ($conn->query($sql) === TRUE) {
    echo "更新成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

1.5 删除数据

$sql = "DELETE FROM your_table WHERE id=1";
if ($conn->query($sql) === TRUE) {
    echo "删除成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

1.6 关闭数据库连接

$conn->close();

2. 使用PDO操作数据库:

首先,确保已经安装了PDO扩展。然后,按照以下步骤操作数据库:

2.1 创建数据库连接

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

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

2.2 执行SQL查询

$sql = "SELECT * FROM your_table";
try {
    $stmt = $conn->prepare($sql);
    $stmt->execute();

    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    foreach($stmt->fetchAll() as $row) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} catch(PDOException $e) {
    echo "Error: " . $sql . "<br>" . $e->getMessage();
}

2.3 插入数据

$sql = "INSERT INTO your_table (name, email) VALUES (:name, :email)";
try {
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $name = "John Doe";
    $email = "john@example.com";
    $stmt->execute();
    echo "新记录插入成功";
} catch(PDOException $e) {
    echo "Error: " . $sql . "<br>" . $e->getMessage();
}

2.4 更新数据

$sql = "UPDATE your_table SET name=:name WHERE id=:id";
try {
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':id', $id);
    $name = "Jane Doe";
    $id = 1;
    $stmt->execute();
    echo "更新成功";
} catch(PDOException $e) {
    echo "Error: " . $sql . "<br>" . $e->getMessage();
}

2.5 删除数据

$sql = "DELETE FROM your_table WHERE id=:id";
try {
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':id', $id);
    $id = 1;
    $stmt->execute();
    echo "删除成功";
} catch(PDOException $e) {
    echo "Error: " . $sql . "<br>" . $e->getMessage();
}

2.6 关闭数据库连接

$conn = null;

以上示例展示了如何使用MySQLi和PDO扩展在PHP中操作数据库。根据您的需求和项目结构,可以选择适合您的方法。

0