PHP Mysqli从数据库删除

时间:2018-10-01 07:08:54

标签: php mysqli sql-delete

因此,我认为这将是一个简单的查询,只是删除某些列下未存储任何数据的行,但是由于某种原因,我的查询返回的是删除了零行,因此我检查了表,还在那儿。

我要从gps_routes表中删除,其中route_latroute_long不包含位置(空)。

我已经检查过我,以确保我也启用了删除权限。

$sql = "SELECT * FROM gps_routes";
$result = $link->query($sql);
$rowCount = $result->num_rows; $rows_deleted = 0; $delete_row = false;

if ($rowCount > 0)
{

 while($row = $result->fetch_assoc())
 {
      $user = $row['user_email'];
      $id = $row['route_id'];
      $lat = $row['route_lat'];
      $lng = $row['route_long'];

      if (empty($lat) || empty($lng)){
        $delete_row = true;
      }

      if (ctype_space($lat) || strlen(trim($lat)) == 0){
        $delete_row = true;
      }

      if ($lat == '' || $lat == ""){
        $delete_row = true;
      }

      if ($delete_row){
        $rows_deleted++;
        mysqli_query($link, "DELETE FROM gps_routes WHERE user_email = '$user' AND route_id = '$id'");
     }
  }
     echo "Routes deleted: $rows_deleted";
}

1 个答案:

答案 0 :(得分:3)

从您的代码中建议您只想遍历数据库并检查lat和long是否为空。如果要删除它们。

听起来像您可以使用此查询来完成工作。

mysqli_query($link, "DELETE FROM gps_routes WHERE (route_lat = '' OR route_lat IS NULL) OR (route_long = '' OR  route_long IS NULL)");

这是根据您提供的代码来执行的操作:

$query = "DELETE FROM gps_routes WHERE (route_lat = '' OR route_lat IS NULL) OR (route_long = '' OR  route_long IS NULL)";
$result = $link->query($query);

echo 'Routes deleted: ' . $result->num_rows;