php
  • mysql
  • 2010-07-02 120 views 1 likes 
    1

    出于某种原因,我想删除我的表中使用php mysql查询函数的一些记录。这是我写的在mysql中执行多个sql删除查询for php

    $sql = "delete from progress where first_date='2010-01-01' and last_date='2010-01-31'; 
    delete from progress where first_date='2010-02-01' and last_date='2010-02-28'; 
    delete from progress where first_date='2010-03-01' and last_date='2010-02-31'; 
    delete from progress where first_date='2010-04-01' and last_date='2010-02-30'; 
    delete from progress where first_date='2010-05-01' and last_date='2010-02-31';"; 
    
    if(!mysql_query($sql)) echo "Error deleting records"; 
    

    这正是我得到的,“错误删除记录”。但是,如果使用mysql_error()来使用它,毕竟它没有用。任何人都知道如何处理这个?感谢之前

    +0

    把你最后的回声改为'echo'删除记录时出错:',mysql_error();',它会吐出发生的确切错误。只是说“发生了什么事”是没有用的。 – 2010-07-02 14:54:47

    回答

    2

    使用mysql_query()时,您无法发送多个SQL查询。这就是它失败并返回错误的原因。

    +0

    这就是我以前所做的。奇怪的是,当我使用phpmyadmin时间过长看起来很好 – 2010-07-02 10:36:40

    +1

    我认为这是因为phpMyAdmin解析你的SQL,按给定的分隔符分割语句并为每个单独的语句运行'mysql_query()'一次。 – BoltClock 2010-07-02 10:47:31

    +0

    我会再试一次谢谢之前 – 2010-07-02 10:52:23

    5

    出于安全原因,mysql_query()不能执行多个语句。

    使用mysqli_multi_query()或多次调用mysql_query()。

    +0

    是的,我使用mysqli_multi_query,但一旦我得到错误:'命令不同步,你现在不能命令'或smiliar – 2010-07-02 10:39:00

    +0

    我会再试一次谢谢 – 2010-07-02 10:46:34

    0

    您可以为每个查询创建不同的变量,然后为每个创建的变量调用mysql_query。 我这样做时,我想执行两个查询来选择和更新一次。

    0

    为什么不把所有查询合并成一个?

    $ sql =“DELETE FROM progress WHERE(first_date ='2010-01-01'AND last_date ='2010-01-31')OR(first_date ='2010-02-01'AND last_date ='2010- 02-28')OR(first_date ='2010-03-01'AND last_date ='2010-02-31')OR(first_date ='2010-04-01'AND last_date ='2010-02-30')或(first_date ='2010-05-01'AND last_date ='2010-02-31')“;

    +0

    我现在会使用PDO,而不是mysql_query 。但是你的代码可能会反过来。 – 2017-04-13 09:13:37

    相关问题