我的数据库中有一个如下所示的表:
|id|team_name|team_url|xml|
我有一个调用脚本的 cronjob。在此脚本中,我想使用我的类来检查 url 是否存在,如果不存在,则删除数据库中的条目。像这样的事情:
foreach row in table, if (Security::checkUrl(team_url)), delete entry. else: update xml.
我怎样才能做这样的事情?我不需要 url 验证方面的帮助,只需要 mysql 查询以及我应该如何遍历每一行并删除 url 无效的行。
谢谢。
最佳答案
删除该行的 mysql 查询是
DELETE FROM tablename WHERE team_url = '$team_url';
$team_url 是具有 team_url 值的 php 变量。
上述命令将删除 team_url 与 $team_url 匹配的所有行。
您需要做的是在 php 中循环遍历所有行并检查它们的 URL。
$query = "SELECT * FROM tablename";
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
if (Security::checkUrl($row['team_url'])) {
$res = mysql_query("DELETE FROM tablename WHERE team_url = '".mysql_real_escape_string($row['team_url'])."'");
}
else {
//update xml
}
}
mysql_free_result($result);
上面的代码只是一个示例,如果没有适当的 SQL 注入(inject)清理/检查,请勿在生产中使用。
关于php - mysql新手问题php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3728541/