doushuo8677 2014-08-25 18:11
浏览 45
已采纳

从MySQL / PHP中的多个表中删除的正确语法

Each of the below lines work individually but I would like to combine them into one query.

$query4="DELETE FROM a WHERE email = '$email'";
$query2="DELETE FROM b WHERE email = '$email'";
$query3="DELETE FROM c WHERE email = '$email'";
$query1="DELETE FROM d WHERE email = '$email'";
$result = mysqli_query($con, $query[1-4, depending]) or die(mysqli_error($con));

I have tried a number of different ways. From the MySQL docs, it should be as simple as:

$query="DELETE FROM a, b, c, d WHERE email = '$email'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));

I have also tried it with backticks, as:

$query="DELETE FROM `a`, `b`, `c`, `d` WHERE email = '$email'";

And I tried concatenating the individual four lines into a long semi-colon delimited string.

Everything but the four individual lines gives me a SQL syntax error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE email = 'xxxxxx@yyyyyyyyy.com'' at line 1.

I checked that the db user has DELETE permissions (and of course they do, since it works fine when run individually).

It's relatively simple to do this in separate statements but I'm just sort of baffled as to why it's not working at this point. Would like to solve this just for curiosity's sake.

I did find the mysqli_multi_query() command...is the "...a, b, c, d..." part being seen by mysqli as separate queries?

Thank you for any help you can provide! (Please note I've left out db connection lines as it's connecting fine.) Again...thank you!

  • 写回答

3条回答 默认 最新

  • dreamworld2007 2014-08-25 18:35
    关注

    As per OP's request:

    "@Fred-ii- Thank you, this worked. I concatenated the separate statements rather than trying to write one, then used mysqli_multi_query. If you would like to write this as an answer I'd be happy to mark it!"

    Yes, use multi_query

    Instead of $query1, $query2 etc. Concatenate it just as the manual shows.

    Use $query .= for all of them, but don't use a dot for the first one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上