Error #1064 while executing sql query in phpmyadmin (wamp server) - php

m trying to make simple login registration app in android having php mysql db at the back end.. when i try to execute following query, i get error in phpmyadmin
Code:
create database android_api /** Creating Database **/
use android_api /** Selecting Database **/
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
); /** Creating Users Table **/
Error:
#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 'use android_api /** Selecting Database **/
create table users(
id int(11' at line 3
also i'd like to know.. the tutorial m following says i need two machines on same wifi network to run php n android.. cant they be run on same machine?? how about after deployment??

try the following. Creates a db to keep it out of yours for a test.
create schema abc123; -- create a db
use abc123; -- use it
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null,
name varchar(50) not null,
email varchar(100) not null,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime null,
updated_at datetime null,
unique key(unique_id),
unique key(email)
); /** Creating Users Table **/
drop schema abc123; -- clean up drop schema

Run this:
create database android_api;
use android_api;
create table users(
id int(11) primary key auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);

Related

MariaDB create database relational tables

I created 2 tables as follows:
$sql = "CREATE TABLE users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(25) UNIQUE NOT NULL,
password VARCHAR(256) NOT NULL,
first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,
email varchar(50) UNIQUE NOT NULL
date_created VARCHAR(25) NOT NULL,
date_modified VARCHAR(25) NOT NULL
)";
This table was created successfully.
I tried to create a 2nd table as follows:
$sql = "CREATE TABLE users_performance (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT(11) REFERENCES users(id) UNIQUE NOT NULL,
performance text NOT NULL
)";
This failed with following error:
Error creating table: 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 'NOT NULL, performance text NOT NULL )' at line 3
Now the error line is:
user_id INT(11) REFERENCES users(id) UNIQUE NOT NULL
I have scratched through Maria DB manual but can't quickly get a solution.
I know this syntax works fine on postgresql. Why is it not working on mysqli
I prefer to create 2 separate tables for future changes, etc.
Thanks
First, check that you are using MariaDB 10.5 or later. Inline foreign keys are not implemented in older versions of MariaDB.
Then, move the UNIQUE NOT NULL before the foreign key option. The order of column options is important.
Also, make sure the data type of the foreign key column is the same as the column it references. In this case, you need to make it INT UNSIGNED. The "length" option (11) doesn't matter, and you can omit it.
user_id INT UNSIGNED UNIQUE NOT NULL REFERENCES users(id),
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=54964388cd0d880169c0332934c13b3d

PDO query updating a datetime column not in query Part 2

Continuation from this question:
PDO query updating a datetime column not in query
A column in my table called lastLoginDate was being automatically updated even though my prepared statement did not include said column.
Apparently, when I created the new column, a trigger was set.
Upon using the command SHOW CREATE TABLE table_name, I returned the following results:
CREATE TABLE `users_edi` (
`username` varchar(30) NOT NULL DEFAULT '',
`fullname` varchar(50) DEFAULT NULL,
`userlevel` tinyint(1) unsigned NOT NULL,
`ipaddress` varchar(30) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`entrydate` datetime DEFAULT NULL,
`division` varchar(35) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`userid` varchar(32) DEFAULT NULL,
`timestamp` int(11) unsigned NOT NULL,
`job_title` varchar(30) DEFAULT NULL,
`dept` varchar(50) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`lastLoginDate` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, // <-- here
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The table is years old. I just created the column and somehow, a trigger was set to it (I guess).
Regardless, I tried to remove it using the following command:
ALTER TABLE `users_edi`
`lastLoginDate` datetime DEFAULT NULL
But I only get the following error:
[Err] 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 '`lastLoginDate` datetime DEFAULT NULL' at line 4
How do I remove this trigger using the ALTER TABLE command or any other command?
ALTER TABLE users_edi MODIFY COLUMN lastLoginDate DATETIME DEFAULT NULL;
You might like to read this page on ALTER TABLE: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

While importing Database file in myPHP Admin I'm getting error CREATE TABLE IF NOT EXISTS `wpcp_2_aiowps_event

While importing a Database file in myPHP Admin I'm getting the following error:
CREATE TABLE IF NOT EXISTS wpcp_2_aiowps_events ( id bigint(20) NOT NULL AUTO_INCREMENT, event_type varchar(150) NOT NULL DEFAULT '', username varchar(150) DEFAULT NULL, user_id bigint(20) DEFAULT NULL, event_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00', ip_or_host varchar(100) DEFAULT NULL, referer_info varchar(255) DEFAULT NULL, url varchar(255) DEFAULT NULL, country_code varchar(50) DEFAULT NULL, event_data longtext, PRIMARY KEY (id) ) TYPE=MyISAM AUTO_INCREMENT=1 MySQL said: Documentation #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 'TYPE=MyISAM AUTO_INCREMENT=1' at line 13
How can I solve this and import the data successfully without any errors
Note
The older TYPE option was synonymous with ENGINE. TYPE was deprecated in MySQL 4.0 and removed in MySQL 5.5. When upgrading to MySQL 5.5 or later, you must convert existing applications that rely on TYPE to use ENGINE instead.
So you should use
CREATE TABLE IF NOT EXISTS wpcp_2_aiowps_events ( id bigint(20) NOT NULL AUTO_INCREMENT, event_type varchar(150) NOT NULL DEFAULT '', username varchar(150) DEFAULT NULL, user_id bigint(20) DEFAULT NULL, event_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00', ip_or_host varchar(100) DEFAULT NULL, referer_info varchar(255) DEFAULT NULL, url varchar(255) DEFAULT NULL, country_code varchar(50) DEFAULT NULL, event_data longtext, PRIMARY KEY (id) )ENGINE = MyISAM ;

Creating Tables with MYsql in yii 1.1

Here is the scenario: I have been following the "yii" book by Larry Ullman in which he gave his MYsql for CMS but he didn't described any tool , how to create these tables of sql. The only way I know is through migrations but the sql written in the book is not working in migrations.
The sample sql is given for a table from the book :
CREATE TABLE IF NOT EXISTS yii_cms.user ( id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(45) NOT NULL,
email VARCHAR(60) NOT NULL,
pass CHAR(64) NOT NULL,
type ENUM('public','author','admin') NOT NULL,
date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE INDEX username_UNIQUE (username ASC),
UNIQUE INDEX email_UNIQUE (email ASC) )
ENGINE = InnoDB DEFAULT
CHARACTER SET = utf8
If you don't mind, you can use straight-forward way to execute sql so:
$sql = 'your sql here';
Yii::app()->db->createCommand($sql)->execute();
The other way is to use Yii QueryBuilder and the createTable() command. More information for QueryBuilder is here and for createTable() here.

how to maintain records current as well as previous in mysql

Hi any one please help i have a contact table in which i can Insert,Delete,Modify database using PHP web pages....but only current changes will be updated to database. what i want is how i can maintain history of database...
Is there any tutorial for this using (PHP/MYSQL).
I tried creating version of MySQL table for patient... how to proceed further.
CREATE TABLE IF NOT EXISTS `contact` (
`name` varchar(30) NOT NULL,
`phone` varchar(12) NOT NULL,
`mobile` varchar(12) NOT NULL,
`email` varchar(30) NOT NULL,
`address` text NOT NULL,
`conid` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`conid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; # MySQL returned an empty result set (i.e. zero rows).
CREATE TABLE IF NOT EXISTS `contactversion` (
`name` varchar(30) NOT NULL,
`phone` varchar(12) NOT NULL,
`mobile` varchar(12) NOT NULL,
`email` varchar(30) NOT NULL,
`address` text NOT NULL,
`conid` int(11) NOT NULL,
`revision_id` int(11) AUTO_INCREMENT,
type ENUM('INSERT', 'UPDATE', 'DELETE') NOT NULL,
`change_time` DEFAULT current_timestamp,
PRIMARY KEY (`revision_id`)
);
what to do next....
When running the queries to contact, just simply run this right before to take the current contact and copy it in your revision table...
"INSERT INTO
contactversion (name,phone,mobile,email,address,conid,type)
SELECT
name,phone,mobile,email,address,conid,'".$type."' as type
FROM contact
WHERE conid='".$conid."'"
Both tables will require to be identical, with contactversion having type and change_time as additionnal last columns.
It is obvious that this query should be ran before UPDATE and DELETE of the contact table, but after an INSERT. If you are updating multiple contacts with another where clause than the conid, you'll want to consider building the where statement in a variable to use it inside the INSERT's SELECT and the UPDATE/DELETE
While creating contactversions table make sure conid should not be primary key and auto incremented. I hope that is causing the problem.

Categories