What's wrong with this PHP-MySQL CREATE TABLE query? - php

First, I'm just starting to learn MySQL with PHP.
My query copy/paste directly from my IDE:
$query = "CREATE TABLE IF NOT EXISTS $table_messages (
id int(11) unsigned NOT NULL auto_increment,
show tinyint(1) unsigned NOT NULL default '0',
to varchar(255) NOT NULL default '',
from varchar(255) NOT NULL default '',
type varchar(255) NOT NULL default '',
message varchar(255) NOT NULL default '',
PRIMARY KEY(id)
) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
$result = mysql_query( $query, $link ) OR exit ( mysql_error() );
Results in this error:
You have an error in your SQL syntax;
near 'show tinyint(1) unsigned NOT
NULL default '0' , to varchar(255) N'
at line 4
... so I add one character to show (e.g. showz) and get this error:
You have an error in your SQL syntax;
near 'to varchar(255) NOT NULL
default '' , from varchar(255) NOT
NUL' at line 5
... so I add one character to to (e.g. toz) and get this error:
You have an error in your SQL syntax;
near 'from varchar(255) NOT NULL
default '' , type varchar(255) NOT NU'
at line 6
... so I add one character to from (e.g. fromz) and IT WORKS!?
What is going on? Lol
If this question is too blatantly obvious, I'll remove it if the community thinks it would be prudent, but in the meantime I'm stumped.
BTW, I've messed with spacing, case and other things without any success.

SHOW, TO and FROM are reserved MySQL keywords. You must quote them with backticks to make them work as column names:
$query = "CREATE TABLE IF NOT EXISTS $table_messages (
`id` int(11) unsigned NOT NULL auto_increment,
`show` tinyint(1) unsigned NOT NULL default '0' ,
`to` varchar(255) NOT NULL default '' ,
`from` varchar(255) NOT NULL default '' ,
`type` varchar(255) NOT NULL default '' ,
`message` varchar(255) NOT NULL default '' ,
PRIMARY KEY(id)
) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
It's usually good practice (though unneeded) to quote every column name this way to prevent accidental collisions with keywords as there are hundreds of them. For a full list, see http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html.

You might be interested in this list of reserved words in MySQL statements. In short, if you want to use any of these as a column name (or anywhere in following queries), you have to quote them, usually in backticks:
`show` TINYINT(1) UNSIGNED NOT NULL,
...and later:
SELECT `show` FROM `varchar` WHERE `to`="France"

Just a stab in the dark, but are to and from reserved words in mysql? Could you either wrap those words in [] like [to] and [from] or, like you did, change the terms to toperson or fromperson?

This is not the answer to your problem, but it's the answer to "What's wrong with a PHP-MySQL CREATE TABLE query?" (for another googler)
Maybe not all versions of PHP are like this, but in my environment, non PDO commands like "mysql_query" throws an error when I try to make a table:
CREATE TABLE IF NOT EXISTS `actionlog`
Error:
You have an error in your SQL syntax
Works just fine with the PDO adapter.

Related

VBulletin 5.4.0 installation Error: 1071

Today I Was Installing Vbulletin For The First Time And During The Installation, It Gave This Error.
Creating userchangelog table An error has occurred with your database.
Please contact vBulletin Support for assistance.
Module: install, Step 3 Database Error:1071
Specified key was too long; max key length is 1000 bytes
Query:
CREATE TABLE userchangelog ( changeid INT UNSIGNED NOT NULL
AUTO_INCREMENT, userid INT UNSIGNED NOT NULL DEFAULT '0', fieldname
VARCHAR(250) NOT NULL DEFAULT '', newvalue VARCHAR(250) NOT NULL
DEFAULT '', oldvalue VARCHAR(250) NOT NULL DEFAULT '', adminid INT
UNSIGNED NOT NULL DEFAULT '0', change_time INT UNSIGNED NOT NULL
DEFAULT '0', change_uniq VARCHAR(32) NOT NULL DEFAULT '', ipaddress
INT UNSIGNED NOT NULL DEFAULT '0', PRIMARY KEY (changeid), KEY
userid (userid,change_time), KEY change_time (change_time), KEY
change_uniq (change_uniq), KEY fieldname (fieldname,change_time),
KEY adminid (adminid,change_time) ) ENGINE = MyISAM Status: Aborted
Version VBulletin: 5.4.0 PHP: 7.0 MYSQL: I Have No Idea!
Before The Installation, I Run A Script Called vb_test.php The Result Said That VBulletin 5.4.0 Can Run On This Server Without Any Error
From the failed error, it seems that you are using MyISAM engine. Better to change the default storage engine of the mysql server to Innodb and also set the innodb_large_prefix to ON.
To set the default storage engine and to enable innodb_large_prefix, you can add the below line in my.cnf
default_storage_engine = innodb
innodb_large_prefix = ON
After this, try to install and see if it works.

Error Importing MySQL with JSON Data as column

I have an error while I am importing a db structure in MySQL
Error:
[ERROR in query 14] 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 'json DEFAULT NULL, account_name char(255)
DEFAULT NULL, account_user_id ' at line 3 Import cancelled!
The whole exact SQL command is as follows:
CREATE TABLE ees_account ( account_business_id int(32) NOT NULL
AUTO_INCREMENT, data json DEFAULT NULL, account_name char(255)
DEFAULT NULL, account_user_id int(32) DEFAULT '0', created_at
timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT
NULL, account_status int(32) DEFAULT '1', account_description
text, account_website char(255) DEFAULT NULL, account_address
char(255) DEFAULT NULL, account_email char(255) DEFAULT NULL,
account_country_id int(32) DEFAULT NULL, business_reference_id
char(255) DEFAULT NULL, account_client_logo text,
client_reference_id char(255) DEFAULT NULL, PRIMARY KEY
(account_business_id) ) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT
CHARSET=utf8;
Any idea what could be the potential cause ? I check the MySQL version which is same i.e. 5.7.17
Faced a similar issue few months back and the issues was MySQL version. MariaDB 10.2 supported JSON. (Alpha version. Not recommended by Maria to production server. Only testing.)MariaDB 10.1 do not support JSON.
So if you have WAMP or XAMPP dev stack then most probably you have 10.1.16-MariaDB which doesn't support JSON. So upgrade your MySQL and it will be resolved.

Joomla Database import does not work

i made a backup of my joomla MySQL database using the export function of phpMyAdmin.
When i try to import this backup file, it gives me a lot of errors like "You have an error in your SQL syntax..." even if the SQL looks quite correct.
Here is a small part of the backup, which doesnt even work:
CREATE TABLE `jos_assets` (
`id` int(10) UNSIGNED NOT NULL COMMENT 'Primary Key',
`parent_id` int(11) NOT NULL DEFAULT '0'COMMENT AS `Nested set parent.`,
`lft` int(11) NOT NULL DEFAULT '0'COMMENT AS `Nested set lft.`,
`rgt` int(11) NOT NULL DEFAULT '0'COMMENT AS `Nested set rgt.`,
`level` int(10) UNSIGNED NOT NULL COMMENT 'The cached level in the nested tree.',
`name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'The unique name for the asset.\n',
`title` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'The descriptive title for the asset.',
`rules` varchar(5120) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'JSON encoded access control.'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
When i run this command, i get the following error message:
#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 'AS `Nested set parent.`,
`lft` int(11) NOT NULL DEFAULT '0'COMMENT AS `Nested ' at line 3
Can someone explain to me what is wrong here? This code was automatically generated by phpMyAdmin and it looks like valid SQL to me!
Im using:
phpMyAdmin version 4.5.5
PHP version 5.6.14
Server version 10.0.23-MariaDB-0+deb8u1
Joomla version 3.5.0
Ok guys i found out the problem myself:
It seems the line
COMMENT AS `Nested set parent.`,
is no valid SQL. Correct would be
COMMENT 'Nested set parent.',
like you can see in the other lines.
I dont know why phpMyAdmin creates a MySQL Dump with invalid SQL, but this caused the problem.

#1064 Error Message When trying to export local host database online

Hi am an aspiring Computer Scientist. I've been trying to upload my localhost site online for almost a day now. This is what I've done so far:
I created a DB online (from my online hosting acct.), created users and password. I went to my PHPmyAdmin on local host, selected my DB, exported it (a .sql file), edited the URL (find and replace). I went online, to my DB (online) and chose to import DB. I Selected my exported DB file (that of localhost) and clicked Go. I get the following error message:
It tells me:
1064 - 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 ' CREATE TABLE [IF NOT EXISTS] wp_commentmeta (meta_id
bigint(20) u' at line 1
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT =6;
Hope this improves my question?
I really appreciate the work every contributor on this platform has done prior to now. With you guys here at Stack Overflow, I know the nature of my problem, but I've not found a solution that directly applies to it. I can't figure out where the code is not right. Please I need a solution fast, even as people have to register on the site (a WP site) since yesterday.
Thanks to all.
The only way that you would (still) be receiving that error is because you are very most likely using your code via a PHP script, rather than executing it/inserting those codes inside phpmyadmin.
Since you did tag as PHP, that is the only conclusion I can come up with, since using your code in phpmyadmin, did create it successfully.
Minus the brackets of course, as I stated in comments.
Remove the brackets in here [ IF NOT EXISTS ].
You must've have taken the MySQL example literally from their site and used them.
http://dev.mysql.com/doc/refman/5.7/en/create-table.html
Those aren't meant to be used, but as an option.
Plus, if executing from a PHP script, it could be a quotes issue and using the wrong ones.
If you are attempting to, or wanting to create the table through a PHP script, an example of this would be:
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
However, phpmyadmin does have an "IMPORT" option.
Open phpMyAdmin by clicking the phpMyAdmin icon
Select the database in the left menu that you will be working with
Click Import in the top menu
Under File to Import, click Browse and select the backup file you previously created (it will be either a .sql .zip or .tar.gz file).
Click Go at the bottom right
When the database has been imported successfully, you should see a message at the top of the page similar to: Import has been successfully finished, ## queries executed.
Pulled from http://www.inmotionhosting.com/support/website/phpmyadmin/import-database-using-phpmyadmin
Also consult:
http://www.techrepublic.com/blog/smb-technologist/import-and-export-databases-using-phpmyadmin/
which talks about WordPress.
and
https://codex.wordpress.org/Restoring_Your_Database_From_Backup
Another possible reason could be that there may be some type of unicode (hidden) character that was introduced, and/or a file encoding issue.
TBH; at this point in time, I am out of ideas.
Edit:
As an option, you can use the following to be run as a PHP script if it's still giving you problems:
Sidenote: This was pre-tested. Replace the xxx with your own login credentials.
<?php
$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';
$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
die('Connection failed [' . $Link->connect_error . ']');
}
$sql = "CREATE TABLE `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT = 6
";
if ($Link->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $Link->error;
}
Important: Copy this exact code and paste it into the "SQL" option in phpmyadmin and not "IMPORT".
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8mb4_unicode_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci AUTO_INCREMENT =6;
Then click on "GO". That is how I originally tested your code in phpmyadmin.
Are you sure that yr MySQL version is ‘4.1.14’? It's really old.
If it is, the problem is mySQL version.
utf8mb4 encoding is introduced in MySQL 5.5.3 (released in early 2010).
Also Please note that official wordpress docs say:
The utf8mb4 Upgrade
In WordPress 4.2, we’re upgrading tables to utf8mb4, when we can. Your site will only upgrade when the following conditions are met:
You’re currently using the utf8 character set.
Your MySQL server is version 5.5.3 or higher (including all 10.x versions of MariaDB).
Your MySQL client libraries are version 5.5.3 or higher. If you’re using mysqlnd, 5.0.9 or higher.
Also, in the doc there is en explicit reference to wp_commentmeta table.
In our query:
utf8mb4 charset
utf8mb4_unicode_ci collation
wp_commentmeta table
mySQL 4.1.14
Maybe upgranding mySQL can resolve your problems.
I had the same issue when uploading database to server.
Error
SQL query:
#
# Table structure of table `wp_commentmeta`
#
CREATE TABLE `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) COLLATE utf8_unicode_520_ci DEFAULT NULL ,
`meta_value` LONGTEXT COLLATE utf8_unicode_520_ci,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_520_ci;
I Just replace all utf8_unicode_520_ci with utf8_unicode_ci in database. Then I'm able to upload the database. This works for me.
You can try replace all utf8mb4_unicode_ci with utf8_unicode_ci and utf8mb4 with utf8 in database. Hope this will works for u.
When I contacted my host, they helped me resolve the issue. I got this message:
I could see that, in the file 'dbxxx.sql', the command 'Use dbxx;' was missing, this is the reason, you were receiving that error when trying to import the SQL file to the database.
My site is now up and running.

MySQL server version for the right syntax to use near '(10), KEY vid (vid) )' at line 4

Hi I have done an update on my site and got this error!
MySQL server version for the right syntax to use near '(10), KEY vid
(vid) )' at line 4
Coding is done in php 4... and sever is running on php 5
$sql = "CREATE TABLE ".$ine.C_MYSQL_VOTE_IPS." (
vid smallint(5) UNSIGNED NOT NULL,
ip int(10) UNSIGNED NOT NULL default '0',
time timestamp(10),
KEY vid (vid)
)";
Use quotes for field names like ` time ` = .

Categories