使用php删除数据库中的数据

时间:2016-11-08 09:22:36

标签: php mysql

我需要使用PHP代码删除数据库中的数据,我已编写代码但有一些错误消息。 这是php代码(del.php): -

<!DOCTYPE html>
<html>
<body>
<?php

$conn = mysql_connect('localhost', 'root','');

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
} 

$sql = "DELETE FROM register WHERE name='' ";

if ($conn->query($sql) === TRUE)
{
  echo "Record deleted successfully";
} else {
     echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>

数据库名称是&#39; selva&#39;并且表名是&#39; register&#39;,在数据库中文件名是&#34;名称,电子邮件,联系人,地址&#34;,我需要删除姓名或电子邮件或联系人。如何删除!!

1 个答案:

答案 0 :(得分:1)

  

//这将删除整行:

<!DOCTYPE html>
<html>
<body>
<?php

$servername = "localhost";
$username = "root";
$password = "your_password"; // add your pw from the here
$dbname = "selva";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//this will delete the whole row
$sql = "DELETE FROM register WHERE 1 ";

if ($conn->query($sql) === TRUE)
{
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
  

//这将仅删除您在此处设置的COLUMNS:

<!DOCTYPE html>
<html>
<body>
<?php

$servername = "localhost";
$username = "root";
$password = "your_password"; // add your pw from the here
$dbname = "selva";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "ALTER TABLE register DROP Name, DROP Email; DROP Contact; ";

if ($conn->query($sql) === TRUE)
{
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
</body>
</html>


希望它会对你有所帮助:)