php mysqli creat index error - php

I use PHP mysqli to create a table and index.But I got this error.
I have already run these sql in mysql client and it worked well.
$creatTableSql = <<<EOF
CREATE TABLE IF NOT EXISTS duplicate_attachment(
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
md5 VARCHAR(32) NOT NULL,
filesize INT(11),
ids TEXT NOT NULL
);
CREATE UNIQUE INDEX duplicate_attachment_id_uindex ON duplicate_attachment (id);
CREATE UNIQUE INDEX duplicate_attachment_md5_uindex ON duplicate_attachment (md5);
EOF;
$mysqli->query($creatTableSql);
var_dump($mysqli->error);
string(226) "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 UNIQUE INDEX duplicate_attachment_id_uindex ON
duplicate_attachment (id);' at line 8"

This is not a single query but a set of multiple queries.
You should run them one by one.
$mysqli->query("CREATE TABLE IF NOT EXISTS duplicate_attachment(
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
md5 VARCHAR(32) NOT NULL,
filesize INT(11),
ids TEXT NOT NULL
)";
$mysqli->query("CREATE UNIQUE INDEX duplicate_attachment_id_uindex ON duplicate_attachment (id)";
$mysqli->query("CREATE UNIQUE INDEX duplicate_attachment_md5_uindex ON duplicate_attachment (md5)";

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

How to fix SQL error with dropping a table in PHP?

I have created a web you can upload and download files - everything works perfect. But now, I want to create a init file, that delete old records in database and create a new tables in it.
So I write this:
$command = "
IF OBJECT_ID(`".$database.".Users`) IS NOT NULL
DROP TABLE ".$database.".Users;
IF OBJECT_ID(`".$database.".Uploads`) IS NOT NULL
DROP TABLE ".$database.".Uploads;
CREATE TABLE `Users` (
`Id` int(11) NOT NULL,
`User` text NOT NULL,
`Password` text NOT NULL,
`Permission` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
ALTER TABLE `Users` ADD PRIMARY KEY (`Id`);
ALTER TABLE `Users` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=0;
CREATE TABLE `Uploads` (
`Id` int(11) NOT NULL,
`Name` text NOT NULL,
`User` text NOT NULL,
`Comment` text NOT NULL,
`Path` text NOT NULL,
`Permission` int(11) NOT NULL,
`Date` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
ALTER TABLE `Uploads` ADD PRIMARY KEY (`Id`);
ALTER TABLE `Uploads` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=0;
";
$result = mysqli_query($conn, $command) or die(mysqli_error($conn));
I think, that code is right (but obviously not). When I run it, SQL throws an 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 'IF OBJECT_ID(db.Users) IS NOT NULL DROP TABLE db.User' at line 1**.
This means that it don’t have a problem with connection to SQL database.
I tried instead of IF OBJECT_ID use IF NOT EXISTS, but it doesn't works too. Can anybody tell me if multi-line SQL command is this problem or if it is something else?
Note: I use 5.5.37 version of MariaDB (if it helps)
IF is not a valid SQL statement in MySQL / MariaDB.
The IF OBJECT_ID(...) statement in the question appears to be a Transact-SQL (Microsoft SQL Server) construct.
The equivalent functionality in MySQL would be achieved with
DROP TABLE IF EXISTS foo.mytable ;
(I expect this would work in MariaDB 5.5, but I haven't verified.)
Note that if the table doesn't exist, the execution of the statement will raise a warning. (A warning message, not an error message.)
The mysqli_query function runs a single statement. To run multiple statements, we can use mysqli_multi_query function, documented here:
http://php.net/manual/en/mysqli.multi-query.php
As far as concerns, OBJECT_ID does not exist in mysql, only in mssql. Searching for OBJECT_ID mysql 8.0 reference manual does not retun anything meaningful. Even if it existed, your syntax for IF block does not look good : you want IF...THEN...END.
To fix the error, you can replace this :
IF OBJECT_ID(`".$database.".Users`) IS NOT NULL
DROP TABLE ".$database.".Users;
IF OBJECT_ID(`".$database.".Uploads`) IS NOT NULL
DROP TABLE ".$database.".Uploads;
With :
DROP TABLE IF EXISTS ".$database.".Users;
DROP TABLE IF EXISTS ".$database.".Uploads;
never used OBJECT_ID but what you want seem to be easily doable with
"drop table if exists users;"

Is it possible to run a long .sql file through Codeigniter?

I want to run a .sql file from my server in Codeigniter 3.1.0. I have tried following
//code to create a DB & this is successful then following ocde
$query = file_get_contents('./test.sql');
$this->db->query($query);
Here is my test.sql file. https://gist.github.com/rejoan/97dfae1b08116e386b3e6fda97eeb4f7
Now when I run this it shows error always
A Database Error Occurred
Error Number: 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 user ( id int(11) NOT NULL, test_id int(11)
NOT NULL, `' at line 10
CREATE TABLE `test`
( `id` int(11) NOT NULL,
`name` varchar(250) NOT NULL,
`description` text NOT NULL,
`added` date NOT NULL,
`outdate` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `user`
( `id` int(11) NOT NULL,
`test_id` int(11) NOT NULL,
`name` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `test` ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `name` (`name`);
ALTER TABLE `user` ADD PRIMARY KEY (`id`),
ADD KEY `test_id` (`test_id`);
ALTER TABLE `test` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=37;
ALTER TABLE `user` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `user`
ADD CONSTRAINT `FK_user_test` FOREIGN KEY (`test_id`)
REFERENCES `test` (`id`) ON UPDATE NO ACTION;
Filename: C:/xampp/htdocs/spider_clients/system/database/DB_driver.php
Line Number: 691
So how can I resolve this by issue? I have a idea to do this by PHP raw code but Firstly I want to solve by CI. Any Idea?
** Even I have tried by replacing all backtick by single quote still not works
You can't run any SQL file through the API. Not even if you split up the file and run one query per API call.
There are some commands that can appear in SQL files, but they're actually mysql client builtin commands. These commands are not recognized by the SQL parser in the server.
It's tricky to split up the SQL file. There are SQL statements that contain literal semicolons, like CREATE TRIGGER. So you need more complex logic to split up the file, it's not as simple as preg_split('/;/', $query)
Another gotcha: you'll find that submitting a "query" that consists of nothing but an SQL comment causes an error.
Also, if your SQL file is too large, you'll blow out PHP's max memory if you use file_get_contents().
The bottom line is that you'll waste a lot of your time developing this code and trying to make it work. You're better off leveraging the tool that is already designed to run SQL scripts:
shell_exec("mysql databasename < ./test.sql");
See also:
Running MySQL *.sql files in PHP

How to create a table in a database

I am trying to create a table in my database, accountdb. My query to create the table is:
$query1="CREATE TABLE asset( id int(16) auto_increment primary key,TotBalance double(35),creditAmnt double(35),debitAmnt double(35))";
After that when I am executing the above query, the database is created, but there is an error in creating the table. The error is as follows:
error creating tableYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '),creditAmnt double(35),debitAmnt double(35))' at line 1
How can I fix this error?
Here is the correct query:
CREATE TABLE asset(
id int(16) auto_increment primary key NOT NULL,
TotBalance double(35,3),
creditAmnt double(35,3),
debitAmnt double(35,3)
);
When the datatype is double, float, or decimal, you need to specify the decimal places.
Correct syntax to create a double datatype column:
double(D,M);
M is the total number of digits and D is the number of digits following the decimal point.
See also: http://code.rohitink.com/2013/06/12/mysql-integer-float-decimal-data-types-differences/
I hope this helps you.
CREATE TABLE IF NOT EXIST asset(
id int(16) auto_increment primary key
,TotBalance double(35)
,creditAmnt double(35)
,debitAmnt double(35)
);
CREATE TABLE assets (
id int(16) NOT NULL AUTO_INCREMENT,
Tot_balance bigint(20) NOT NULL,
Credit_Amt bigint(20) NOT NULL,
Debit_Amt bigint(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Im trying to create some temporary tables in mysql using php

Hi I'm trying to create some temporary tables in mysql using php
my code:
<?php
mysql_connect("localhost","root","sahan");//database connection
mysql_select_db("callcenter");
$maketemp="create table #temp1 (id varchar(50),date varchar(50),csc varchar(50),effectedareas varchar(50),agent varchar(50))";
$data1 = mysql_query($maketemp)
or die(mysql_error());
?>
but it gives an 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 '' at line 1"
My MySQL version is as below
MySQL Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (i686) using readline 5.1
I'm not much fluent in php coding please can some one help me??
I do not know why you want a table name like that but you need indentifier quotes (backticks) for it to work.
`#temp1`
You need quotes or else it will considered a comment. #
$maketemp = "CREATE TABLE `#temp1` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date` varchar(50),
`csc` varchar(50),
`effectedareas` varchar(50),
`agent` varchar(50),
PRIMARY KEY (`id`)
) COLLATE = 'utf8_general_ci'
";
Please refrain from using mysql_* functions, they are deprecated. Click here.
Here problem is declaring varchar for primay key with auto increment. So make id datatype to int. then its working
$maketemp = "CREATE TABLE `#temp1` (
`id` VARCHAR(50) NOT NULL AUTO_INCREMENT,
`date` varchar(50),
`csc` varchar(50),
`effectedareas` varchar(50),
`agent` varchar(50),
PRIMARY KEY (`id`)
) COLLATE = 'utf8_general_ci'
";
If you want to create temporary table, In my sql, there is temporary table functionality as
http://www.mysqltutorial.org/mysql-temporary-table/
Taken from MySQL Temporary Tables
Temporary tables were added in MySQL version 3.23. If you use an older version of MySQL than 3.23, you can't use temporary tables, but you can use heap tables.
You are mixing up SQLServer syntax with MySQL syntax. While you are able to create a temporary table in SQLServer with #table or a gloabal temporary table with ##table in MySQL you have to use the keyword TEMPORARY
CREATE TEMPORARY TABLE temp1 (
id varchar(50)
,date varchar(50)
,csc varchar(50)
,effectedareas varchar(50)
,agent varchar(50)
)

Categories