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
  • 20,299
  • 35
  • 117
  • 206

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,983
  • 1
  • 24
  • 29
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
  • 246,190
  • 53
  • 318
  • 364
  • 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,826
  • 3
  • 39
  • 63