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条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件