Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL add column with spaces in php [duplicate]

Tags:

php

mysql

I would like to add column to my MySQL-database with spaces.

In terms of SO-questions this is as close as I've come Insert data in mysql colum with spaces with php

In php MyAdmin I can write the code

ALTER TABLE `msrk_krit` ADD `test 1` VARCHAR(255)

However in php I am trying to use the code below:

mysqli_query($db, "ALTER TABLE msrk_krit ADD 'test 1' VARCHAR( 255 )")

But I get this error code:

Error description: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1 VARCHAR( 255 )' at line 1

Any ideas?

Thanks

like image 797
Ola Karlsson Avatar asked Dec 23 '22 15:12

Ola Karlsson


2 Answers

Do this:

mysqli_query($db, "ALTER TABLE msrk_krit ADD `test 1` VARCHAR( 255 )")

Notice that the single quotes around test 1 are actually back ticks, not quote marks.

Honestly though, you should avoid using spaces in your column names, it will be easier to maintain in the long run.

(Documentation: mysqli_query)

like image 108
Difster Avatar answered Jan 07 '23 11:01

Difster


mysqli_query($db, "ALTER TABLE `msrk_krit` ADD `test 1` VARCHAR(255)")
like image 20
prolific Avatar answered Jan 07 '23 09:01

prolific