PHP函數(shù)如何操作數(shù)據(jù)庫

PHP
小樊
81
2024-11-06 13:24:01
欄目: 編程語言

在PHP中,操作數(shù)據(jù)庫通常使用數(shù)據(jù)庫擴(kuò)展(如MySQLi、PDO等)。這里我將向您展示如何使用MySQLi和PDO來操作數(shù)據(jù)庫。

1. 使用MySQLi操作數(shù)據(jù)庫:

首先,確保已經(jīng)安裝了MySQLi擴(kuò)展。然后,按照以下步驟操作數(shù)據(jù)庫:

1.1 創(chuàng)建數(shù)據(jù)庫連接

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

// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname);

// 檢查連接
if ($conn->connect_error) {
    die("連接失敗: " . $conn->connect_error);
}

1.2 執(zhí)行SQL查詢

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

if ($result->num_rows > 0) {
    // 輸出數(shù)據(jù)
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 結(jié)果";
}

1.3 插入數(shù)據(jù)

$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 更新數(shù)據(jù)

$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 刪除數(shù)據(jù)

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

1.6 關(guān)閉數(shù)據(jù)庫連接

$conn->close();

2. 使用PDO操作數(shù)據(jù)庫:

首先,確保已經(jīng)安裝了PDO擴(kuò)展。然后,按照以下步驟操作數(shù)據(jù)庫:

2.1 創(chuàng)建數(shù)據(jù)庫連接

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

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 設(shè)置 PDO 錯(cuò)誤模式為異常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "連接失敗: " . $e->getMessage();
}

2.2 執(zhí)行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 插入數(shù)據(jù)

$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 更新數(shù)據(jù)

$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 刪除數(shù)據(jù)

$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 關(guān)閉數(shù)據(jù)庫連接

$conn = null;

以上示例展示了如何使用MySQLi和PDO擴(kuò)展在PHP中操作數(shù)據(jù)庫。根據(jù)您的需求和項(xiàng)目結(jié)構(gòu),可以選擇適合您的方法。

0