Why does combining my SQL queries into one not work? [duplicate] - php

This question already has answers here:
PHP: multiple SQL queries in one mysql_query statement
(4 answers)
Closed 8 years ago.
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?

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

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.

Multiples queries are not supported in this function.
http://php.net/manual/en/function.mysql-query.php

Related

Is there any need to use prepare statement in select query? [duplicate]

This question already has answers here:
Should we always use prepared statements in MySQL and php or when to use these?
(2 answers)
Closed 22 days ago.
I use often simple method in select query and i do not use prepare statement because i think there is no need to use prepare statement in select because in select we view the table . further tell me I think right or we should use prepare statment in select query also?
If yes please Tell me
it is not necessary when there is no user input. It can sometimes still be useful to use a prepared statement when there is input though, even if it's not user input. This is because preparing a statement allows it to be executed more efficiently if it is run lots of times with different data each time
My mother says: the mountain that can be seen is not far away, don't stop trying

is it better to build sql or while loop in PHP / MySql [duplicate]

This question already has answers here:
Multiple MYSQL queries vs. Multiple php foreach loops
(5 answers)
Closed 6 years ago.
I've always used simple sql update statements inside a loop when updating multiple records. This means the sql is quick but the database is connected to multiple time.
Lately I've been wondering if it would be much better to use a loop to build up a sql statement that does multiple updates in one go.
Does it make much of a difference? I'm using PHP 5.6 & MySql.
Thank you.
The best solution would is to avoid loops altogether.
If you absolutely need to use a loop, you better do it in the database side, as this will eliminate the overhead related to the PHP/database communication.
As documented under Optimizing UPDATE Statements:
Another way to get fast updates is to delay updates and then do many updates in a row later. Performing multiple updates together is much quicker than doing one at a time if you lock the table.
You may also find the page on Optimizing INSERT Statements helpful and informative.
To summarise, in the most general terms: connecting once and sending a single UPDATE statement is better than connecting once and sending multiple UPDATE statements, which in turn is better than connecting multiple times to send a single UPDATE statement on each connection.
Beyond that, there are a host of techniques that can be harnessed to optimise further—but we would need to know the specific configuration and requirements at hand.

When to use views and when to use stored procedures [duplicate]

This question already has answers here:
MySQL: Views vs Stored Procedures
(4 answers)
Closed 9 years ago.
I am a application where number of reports are more. What i do for each report is that i create a mysql view and a mysql stored procedure. From front end php i give a call to stored procedure with where clause, based on this where clause i fetch results from the particular view. Recently i found out that it was causing performance issue. So i avoided views and wrote the same code in stored procedure and performance improved. So from that poit i am confused as to ideal situation when i should use Stored procedure and when i should use views.
And Does my scenario explained above really cause performance issue or was it problem at my end?
Views in mysql are mainly for readability. They enable you to hide a possibly complex query over multiple tables into something that appears to be a single table.
I would suspect that the most likely cause is not the use of views themselves (although not sure they would help you in any way, while a stored procedure might well be more efficient), rather a view you are using is poorly optimised (maybe ignoring keys).

PHP: mysql_query without waiting [duplicate]

This question already has answers here:
How to send a SQL query to database in PHP without waiting for result
(3 answers)
Closed 9 years ago.
Firstly I'm using the deprecated mysql functions (instead of mysqli) knowingly, so please do not tell me I should change to mysqli.
My question is: if I want to do an INSERT or UPDATE and continue processing the PHP script immediately, without waiting for MySQL to complete the task, can I use mysql_unbuffered_query (is that what is does?) or if not, how can I achieve that?
Sorry to break this to you :)
If you use mysqli, with the mysqlnd driver, you can pass a MYSQLI_ASYNC option to the query() method. Unbuffered queries do not help here.
Later on you can use the poll() and reap_async_query() to get to the result.
You can use INSERT...DELAYED for asynchronous insertions (1). I do not believe you can do asynchronous UPDATE's without resorting to spawning another process (2).
(1) but is not available to InnoDB tables
(2) if sticking to the old mysql extension is an absolute requirement

How can I insert data into two mysql tables at once using PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
mySQL - Insert into three tables
I want to know if I can insert data into two tables using one query in PHP/MySQL?
If it's possible, can I see an example?
No, it is not possible. Mysql INSERT syntax accepts one and only one table as a target
using one query
no
Using one query! I don't think so.
However, I believe that the most appropriate methodology is to utilise transactions. Using transactions, you assure to have data inserted in more than one table (successfully), otherwise any changes are rolled back. That is, either persist all changes or do nothing at all.

Categories