SELECT INTO and the "Undeclared Variable" Error
When attempting to execute a query that uses the SELECT INTO syntax to insert data into a variable as follows:
SELECT id_subscriber INTO newsletter_to_send FROM subscribers
An error "1327 - Undeclared variable: newsletter_to_send" might be encountered. This error occurs because MySQL does not support SELECT INTO for variables.
Solution
To insert data into a table using the result of a query, the INSERT ... SELECT syntax should be used instead:
INSERT INTO newsletter_to_send SELECT id_subscriber FROM subscribers
This query will insert the id_subscriber values from the subscribers table into the newsletter_to_send table.
Additional Considerations
When using INSERT ... SELECT, consider including a WHERE clause to filter the data being inserted. Additionally, ensure that the table specified in the INTO clause already exists before executing the query.
The above is the detailed content of Why Am I Getting an \"Undeclared Variable\" Error with SELECT INTO in MySQL?. For more information, please follow other related articles on the PHP Chinese website!