Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when importing mysql table with html code in it

Im trying to import a mysql table with data that has html code in it and it generates syntax error. Can someone tell me how to properly import mysql with html code

Sytanx error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''<ul>\r\n<li>Unpack the equipment and materials</li>\r\n<li>Secure the mounting '
like image 259
Cadz Avatar asked Apr 18 '18 11:04

Cadz


People also ask

How do I import data into MySQL?

Importing a database from a file To import a file, open Workbench and click on + next to the MySQL connections option. Fill in the fields with the connection information. Once connected to the database go to Data Import/Restore. Choose the option Import from Self-Contained File and select the file.

How do I import a MySQL table into terminal?

To import a table into database, if table is already exist then add this line in your sql file DROP TABLE IF EXIST 'table_name' and run command mysql -u username -p database_name < import_sql_file. sql . Please verify the sql file path before execute the command.


1 Answers

By the very little you have shown us the error i can see that it's complaining about a rouge single quote '

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''<ul>\r\n<li>Unpack the equipment and materials</li>\r\n<li>Secure the mounting '

If you notice at the start of the quoted text from MySQL it has '' and at the end it only has ' MySQL errors show the problem code at the start location in single quotes so we remove one single quote wrapping the errored syntax we end up with

'<ul>\r\n<li>Unpack the equipment and materials</li>\r\n<li>Secure the mounting

So it's complaining about a single quote before a <ul>. you need to make sure you HTML is MySQL safe before entering it into the Database so replace single quotes as \' or if it's supposed to be rendered you should use &#39;

if your using php you can use addslashes to escape any single quotes ' with \' before entry.

Another option is to stop using directly constructed queries and use prepare & bind_param in your connector as it will then prevent this from happening.

Unfortunately, however, If you're using mysqlimport cli and this is a prebuilt .sql file your gonna have to go through it by hand and fix it I highly recommend a syntax enabled editor to do this MySQL workbench it the best for MySQL syntax

like image 79
Barkermn01 Avatar answered Oct 07 '22 11:10

Barkermn01