在PHP中修改MySQL數據,可以使用MySQLi或PDO擴展連接到數據庫,并執行UPDATE語句來更新數據。以下是一個簡單的示例:
使用MySQLi擴展:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
// 創建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
// 更新數據
$sql = "UPDATE your_table SET column1='new_value' WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "數據更新成功";
} else {
echo "數據更新失敗: " . $conn->error;
}
$conn->close();
?>
使用PDO擴展:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE your_table SET column1='new_value' WHERE id=1";
$conn->exec($sql);
echo "數據更新成功";
} catch(PDOException $e) {
echo "數據更新失敗: " . $e->getMessage();
}
$conn = null;
?>
在上述示例中,將your_table替換為要更新數據的表名,column1替換為要更新的列名,new_value替換為要設置的新值,以及WHERE條件來指定要更新的行。