在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条件来指定要更新的行。