1

These two PHP MySQL queries work.

mysql_query("DELETE FROM videos WHERE id='10';");
mysql_query("DELETE FROM comments WHERE videoId='10';");

This single query fails due to a MySQL syntax error pertinent to the latter DELETE operation.

mysql_query("DELETE FROM videos WHERE id='10';DELETE FROM comments WHERE videoId='10';");

I've stared hard and can't see the syntax error. What is it?

dangerChihuahua007
  • 19,401
  • 30
  • 112
  • 205

3 Answers3

3

Not supported by mysql_query see How can I put two queries in one mysql_query? use http://docs.php.net/mysqli.multi-query

Community
  • 1
  • 1
dotoree
  • 2,965
  • 1
  • 22
  • 28
1

You cannot execute multiple queries with mysql_query. If you really want to (security risk!), use mysql_multi_query. (And you should use the newer mysqli_* functions). It's a good idea two embed those two calls in a transaction.

But this looks a lot like you really want to define foreign key constraints. I highly recommend them, if you are already using InnoDB.

knittl
  • 216,605
  • 51
  • 293
  • 340
  • Thanks! Why is it a security risk to run many queries at once? And indeed, I'm trying to enforce foreign key constraints. – dangerChihuahua007 Mar 03 '12 at 18:56
  • 1
    Security risk, if you use user supplied data in your queries and fail to escape a single value (sql injections). This cannot happen with prepared statements, but I don't think prepared statements allow multiple queries with a single command either. – knittl Mar 03 '12 at 18:57
1

Multiples queries are not supported in this function.

http://php.net/manual/en/function.mysql-query.php

nosbor
  • 2,624
  • 3
  • 37
  • 62