[英]php delete links rows from mysql database
I need to delete some data from mysql. 我需要从mysql中删除一些数据。 I know a bit of php but get really lost with mysql and really don't want to mess this up... I did make a .sql backup but I need to get this right.
我知道一些PHP,但真的迷失了mysql,真的不想搞砸了......我确实做了.sql备份,但我需要做到这一点。 That's why i'm posting the question here.
这就是我在这里发布问题的原因。 I know I will get some crap for not trying myself and posting my code here but to be hosnest.. this is as far as I got before I got stuck:
我知道我会因为没有尝试自己并在这里发布我的代码而得到一些废话但是要保持真实......这是我在被卡住之前所得到的:
<?php
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE", $con);
$result = mysql_query("SELECT * FROM subscriber");
while($row = mysql_fetch_array($result))
{
echo $row['mail'] . " " . $row['snid'];
echo "<br />";
}
mysql_close($con);
?>
What i'm really trying to do is this: 我真正想做的是:
status
= 0
the in the table called subscription
and get the field snid
(which is an integer) subscription
的表中找到status
= 0
的所有行,并获取字段snid
(这是一个整数) subscriber
and delete the row with the matching snid
(also a field in this tables row) subscriber
的表并删除带有匹配snid
的行(也是此表行中的一个字段) subscription
and delete the original matched row subscription
并删除原始匹配的行 Does this make sence?.... I've done my own head in just trying to explain it :( 这是否有意义?....我只是想解释一下我自己的头脑:(
要做你所问的,你只需要1个查询:
delete subscriber, subscription from subscriber,subscription where subscription.status = 0 and subscription.snid=subscriber.snid;
尝试这样的事情:
DELETE a,b FROM subscriber AS a INNER JOIN subscription AS b ON a.snid = b.snid WHERE b.status = 0;
if subscriber and subscription are liked by the snid key, you should use a foreign key with cascading on delete operations. 如果snid键喜欢订阅者和订阅,则应该使用带有级联删除操作的外键。
http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
All you need to do is create an if statement in your while loop to check the status, if it's 0 execute the delete on both tables. 您需要做的就是在while循环中创建一个if语句来检查状态,如果它是0则在两个表上执行delete。 Here is the code for your while loop:
这是你的while循环的代码:
while($row = mysql_fetch_array($result))
{
if($row['status']==0) {
if(mysql_query("DELETE FROM `subscriber` WHERE `snid`={$row['snid']}") {
mysql_query("DELETE FROM `subscription` WHERE `snid`={$row['snid']}");
}
}
}
I put an additional if statement on your delete, so that if it does not delete from the subscription table it won't delete from the subscriber table either. 我在你的删除上添加了一个额外的if语句,这样如果它没有从订阅表中删除,它也不会从订阅者表中删除。
$result = mysql_query("SELECT * FROM subscriber WHERE status=0");
while($row = mysql_fetch_array($result)) {
mysql_query('DELETE FROM subscription WHERE snid =' . $row['snid']);
mysql_query('DELETE FROM subscriber WHERE id =' . $row['id']);
}
This should work. 这应该工作。
$result = mysql_query("SELECT * FROM subscription WHERE status = 0;");
while($row = mysql_fetch_array($result))
{
$snid = $row['snid'];
mysql_query("DELETE FROM subscriber WHERE snid = $snid;");
mysql_query("DELETE FROM subscription WHERE snid = $snid;");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.