SELECT INTO and Error: Undeclared Variable
When attempting to execute the query:
SELECT id_subscriber INTO newsletter_to_send FROM subscribers
You encounter an error:
#1327 - Undeclared variable: newsletter_to_send
Cause:
The error arises because the variable newsletter_to_send is not declared or initialized before its use in the query.
Solution:
To resolve this issue, you need to use the INSERT ... SELECT syntax instead of the SELECT INTO syntax. The correct query should be:
INSERT INTO newsletter_to_send SELECT id_subscriber FROM subscribers
The INSERT ... SELECT syntax allows you to insert multiple rows into a table by using the results of a subquery.
Additional Note:
It's worth considering if there is a need for a WHERE clause in the subquery to filter the results and potentially prevent inserting unwanted rows into the newsletter_to_send table.
The above is the detailed content of Why does my query \'SELECT INTO\' result in an \'Undeclared Variable\' error?. For more information, please follow other related articles on the PHP Chinese website!