Getting error while running the mysql query to create table - php

I am getting one error while creating table using MySQL. I am explaining my query below.
$sql="CREATE TABLE db_cron( ".
"id INT NOT NULL AUTO_INCREMENT, ".
"user_alert INT(11) NOT NULL DEFAULT NONE, ".
"status INT(11) NOT NULL DEFAULT NONE, ".
"PRIMARY KEY ( id )); ";
I am getting the below 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 'NONE, status INT(11) NOT NULL DEFAULT NONE, PRIMARY KEY ( id ))'
at line 1
Here I need to create table with default value as none.

You have used too many quotes just to connect multiple string, it does not required.
Please remove those quotes and you also have to provide default value instead of NULL something like 0 OR any other value that you want.
However you can use below query to create table.
$sql = "CREATE TABLE `db_cron`( id INT NOT NULL AUTO_INCREMENT,
user_alert INT(11) NOT NULL DEFAULT 0,
status INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY ( id ))";
above query will create table with default value 0 for user_alert and status
Hope this will helps you
Thanks & Regards.

1.You have applied unnecessary quotes.
2.You have NOT NULL along with DEFAULT NONE, which is not going to happen(You have to provide some value as default when using NOT NULL)
Do like below:-
$sql="CREATE TABLE db_cron(
id INT(11) NOT NULL AUTO_INCREMENT,
user_alert INT(11) NOT NULL DEFAULT 0,
status INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY ( id )
)";

Related

Error: unexpected update count received (Actual: 0, Expected 1)

I have created a table and now find that when I try and delete a row or change a field, I get the following message:
Unexpected update count received (Actual: 0, Expected: 1). All changes will be rolled back.
I saw another question about this that said to create a unique field and try again. I dropped the table, recreated it with a unique field and am still getting the same error.
The code to create the table is:
create table 'transcribercertifications' (
id int(11) not null unique auto_increment,
tid int(11) not null,
certId varchar(32) not null,
regNum int(11) not null,
expiration date
);
I have been deleting and updating 'by hand' in PhpStorm. Any help fixing this would be much appreciated.
You need to make your id column as a primary key. Try this one:
create table 'transcribercertifications' (
id int(11) not null primary key auto_increment,
tid int(11) not null,
certId varchar(32) not null,
regNum int(11) not null,
expiration date
);

SQL syntax error in PHP code, even though command runs in command line

I'm trying to make some SQL commands in XAMPP. My query returns following error:
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 'CREATE TABLE IF NOT EXISTS kayttajat ( id INT(10) NOT NULL
AUTO_INCREMENT, tunn' at line 2
I can't find the syntax error. And when I run the exact same command by copy-paste in command line, it works. So do I need some different syntax in PHP code?
Also if I remove the first command, the error message moves to [...] right syntax to use near 'CREATE TABLE IF NOT EXISTS rivit [...]. If I remove second command the error comes from third command and so on. I really don't understand where the error is.
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS rivit
(
id INT(10) NOT NULL AUTO_INCREMENT,
sivu INT(10) NOT NULL,
kayttaja INT(10) NOT NULL,
sana varchar(500),
kommentti varchar(1000),
aika TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
muutos TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(id)
);
CREATE TABLE IF NOT EXISTS sivut
(
id INT(10) NOT NULL AUTO_INCREMENT,
nimi varchar(32) NOT NULL,
ohje varchar(2000) NOT NULL,
salaisuus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY(id)
);
';
$mysqli->query($query) or die($mysqli->error);
The query() method is designed to execute a single query not multiple ones.
What you are looking for is multi_query() to execute multiple queries separated by a semicolon.
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
...
}
You should use multi_query if you want execute many queries with one command.
Also you don't select database which will used for insert statements.
You should add asdgfhj prefix for all tables or use USE asdgfhj after create table statement.
example with prefix:
<?php
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS asdgfhj.kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
';
$mysqli->multi_query($query) or die($mysqli->error);
example with use statement:
<?php
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
USE asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
';
$mysqli->multi_query($query) or die($mysqli->error);

How to create table in PHP and Oracle

I am trying to create table in PHP and Oracle
$create_table =" CREATE TABLE email_valid(
eid int(11) NOT NULL,
email varchar2(255) NOT NULL UNIQUE,
password varchar2(255) NOT NULL,
activation varchar2(255) NOT NULL UNIQUE,
status enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (eid))";
$stid=oci_parse($conn,$create_table);
oci_execute($stid) or die(oci_error());
But the error occurred:
Warning: oci_execute() [function.oci-execute]: ORA-00907: missing right parenthesis in C:\PORTAL\xampp\htdocs\email\create.php on line 15
I can't find out where the error is? Please help me.
Oracle does not have a sized INT(11) type, if you want to use size, you'll need to use NUMERIC.
Oracle does not have ENUM, but in this case you can use a simple char with a CHECK constraint;
--
CREATE TABLE email_valid(
eid numeric(11) NOT NULL,
email varchar2(255) NOT NULL UNIQUE,
password varchar2(255) NOT NULL,
activation varchar2(255) NOT NULL UNIQUE,
stats char(1) DEFAULT '0' NOT NULL CHECK (stats IN ('0','1')),
PRIMARY KEY (eid)
)
A simple SQLfiddle test.

Why CREATE TABLE IF NOT EXISTS always returns false?

i don't know what i am doing wrong here but i have been trying to get this to work for hours. i just want it to create a table that doesn't exist. i made it as simple as i can make it and still it just returns false and doesn't change anything. please let me what i am doing wrong thank you in advance.
$conn=mysql_connect('localhost:3306', 'root', '');
mysql_select_db("test",$conn);
$sql = "CREATE TABLE IF NOT EXISTS 'works' (
`autoPlace` int(11) unsigned NOT NULL auto_increment,
`element` float(255) NOT NULL,
`month` tinyint(4) NOT NULL ,
`mday` tinyint(4) NOT NULL ,
`wday` char(12) NOT NULL ,
`time` smallint(6) NOT NULL,
PRIMARY KEY (`autoPlace`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$thisKeepsReturningFalse= mysql_query($sql);
var_dump($thisKeepsReturningFalse);
When I tried to execute your query I got this error:
Incorrect column specifier for column 'element'
The problem is float(255) is not a valid declaration
It needs to be float(x) where x<=53, or you can use float(x,y) - see mysql docs here
Also, as pointed out in the comments, you have single quotes around works. Remove them or replace them with backticks.
After fixing these errors, I was able to successfully execute your query.
"CREATE" command in MySQL is not return value command so that it always return false(not like SELECT). To check if the command "CREATE" success or not, you can use "SHOW TABLES" to check after executing the "CREATE" command.
When you create a float element you must specify the decimals with a comma.And also the name of the table can't be with this format '' it must to be inside ``.
So in this example the query should be :
CREATE TABLE IF NOT EXISTS `works`
(
`autoPlace` int(11) unsigned NOT NULL auto_increment,
`element` float(255,0) NOT NULL,
`month` tinyint(4) NOT NULL ,
`mday` tinyint(4) NOT NULL ,
`wday` char(12) NOT NULL ,
`time` smallint(6) NOT NULL,
PRIMARY KEY (`autoPlace`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
Note how the float it's float(255,0) and not just 255. You must set this , 0 is the default

how do i can use # symbol in sqlquery

delimiter $$
CREATE TABLE `users` (
`u_id` int(10) NOT NULL AUTO_INCREMENT,
`ufirstname` varchar(30) NOT NULL,
`ulastname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`pswd` varchar(255) DEFAULT NULL,
`confirmpswd` varchar(255) DEFAULT NULL,
`mobileno` int(12) DEFAULT NULL,
PRIMARY KEY (`u_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8$$
select u_id,ufirstname,mobileno whrer email=test#gmail.com
and i am getting an error:
Error Code: 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 'where' at line 1
please help me
You are missing your FROM clause, you incorrectly spelled where and you didn't surround your string in quotes:
SELECT u_id, ufirstname, mobileno
FROM users
WHERE email = 'test#gmail.com'
Do this:
select u_id,ufirstname,mobileno from users where email='test#gmail.com'
You need to quote the string value
put it in quotes as a String is supposed to
SELECT u_id, ufirstname, mobileno FROM users WHERE email = 'test#gmail.com'

Categories