1

For some reason, i want to delete some of records in my table using php mysql query function. Here's what i've write

$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";

Thats exactly what i get, "Error deleting records". However when itrace it using mysql_error() , its no use after all. Anyone know how to handle this? Thank's before

1
  • Change your final echo there to echo 'Error deleting records: ', mysql_error();, which will spit out the exact error that occured. Just saying "something happened" is of no use.
    – Marc B
    Jul 2, 2010 at 14:54

4 Answers 4

6

mysql_query() can't execute multiple statements for security reasons.

use mysqli_multi_query() or call mysql_query() many times.

1
  • yes i did using mysqli_multi_query but once i got error : 'command out of sync, you can't command now' or smiliar Jul 2, 2010 at 10:39
3

You cannot send more than one SQL query at a time when using mysql_query(). That is why it's failing and returning false.

2
  • that's what i've tought before. It's strange beacuse when i use phpmyadmin everithing looks fine Jul 2, 2010 at 10:36
  • 1
    I think it's because phpMyAdmin parses your SQL, splits statements by the given delimiter and runs mysql_query() once for each separate statement.
    – BoltClock
    Jul 2, 2010 at 10:47
0

you can either create different variables for each query and then call the mysql_query for each variable created. i did it this way when i wanted to execute two queries to select and update at once.

0

Why not combine all queries into one?

$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') OR (first_date='2010-05-01' AND last_date='2010-02-31')";

1
  • I will use PDO nowadays, not mysql_query. But your code might worked back then. Apr 13, 2017 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.