Auto creating table if not existing is causing error - php

I'm trying to create following table . query works in phpmyadmin but doesn't work in php script. pls point out mistake in my code. Thank You
$sql = "CREATE TABLE IF NOT EXISTS `portal` (
 `id` int(255) NOT NULL AUTO_INCREMENT,
 `message_sid` varchar(255) NOT NULL,
 `name` varchar(255) NOT NULL,
 `number` varchar(255) NOT NULL,
 `message` text NOT NULL,
 `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `status` varchar(50) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1";
if (mysqli_query($conn, $sql)) {
echo "Table portal created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
I'm getting this 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 '`id` int(255) NOT NULL AUTO_INCREMENT,
`message_sid` varchar(255) NOT NULL' at line 2

I have done this by checking if table exists in php, on false create a new table
creating table would be best to export an already existing one with phpmyadmin and then just use it as query syntax

Take the single quotes off of your table and row names
also if you make the id a primary key auto-increment, do not make it not null, this will cause a error.
I am unsure why the down votes....
CREATE TABLE IF NOT EXISTS portal (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
message_id VARCHAR(30) NOT NULL,
name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
number VARCHAR(50) NOT NULL,
message text NOT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(50) NOT NULL )ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
This will work

Related

1.6.1.24 update to 1.7.7.4 database schema error [duplicate]

ERROR 1265: Data truncated for column 'profile_pic' at row 1
SQL Statement:
ALTER TABLE `student`.`student_info`
CHANGE COLUMN `profile_pic` `profile_pic` VARCHAR(50) NOT NULL DEFAULT 'images/profile.png'
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'student_info' already exists
SQL Statement:
CREATE TABLE `student_info` (
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`profile_pic` varchar(500) DEFAULT 'images/profile.png',
PRIMARY KEY (`email`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `password_UNIQUE` (`password`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
this error is flashing when i am setting the value of profile_pic column as not null however if i am not not doing so this error is not occurring can any body please explain me why is this error occurring and how to remove this
Change your Alter query to
ALTER TABLE `student`.`student_info`
CHANGE COLUMN `profile_pic` `profile_pic` VARCHAR(500) NOT NULL DEFAULT 'images/profile.png'
In your schema you have previously defined profile_pic with varchar(500) now you are trying to set it not null but with varchar(50) so your column contains the data which is longer than 50 characters therefore you see this truncated error

Unknown column in 'field list' php/mySQL but the field DOES exist.

I have this query in PHP which I call from an Android app.
$sql = "SELECT
`asset`.`idasset`,
`asset`.`idlocation`,
`asset`.`asset_barcode`,
`asset`.`asset_number`,
`asset`.`category_name`,
`asset`.`make`,
`asset`.`model`,
`asset`.`serial_number`,
`asset`.`iduser`,
`asset`.`idcost_centre`,
`asset`.`idcondition`,
`asset`.`idstatus`,
`asset`.`latitude`,
`asset`.`longitude`,
`asset`.`asset_description`
from `asset`";
This is the response I receive from the call in Android Studio console:
D/RAW RESPONSE: Notice: Unknown column 'asset.idlocation' in 'field list'
But that field does exist.
Here's the full create statement I reverse copied to clipboard in MySQLWorkbench.
CREATE TABLE `asset` (
`idasset` int(11) NOT NULL AUTO_INCREMENT,
`idlocation` int(11) NOT NULL,
`asset_barcode` varchar(50) DEFAULT NULL,
`asset_number` varchar(50) DEFAULT NULL,
`category_name` varchar(50) DEFAULT NULL,
`asset_description` varchar(250) DEFAULT NULL,
`make` varchar(50) DEFAULT NULL,
`model` varchar(50) DEFAULT NULL,
`serial_number` varchar(255) DEFAULT NULL,
`iduser` int(11) DEFAULT NULL,
`idcost_centre` int(11) DEFAULT NULL,
`idcondition` int(11) DEFAULT NULL,
`idstatus` int(11) DEFAULT NULL,
`longitude` varchar(45) DEFAULT NULL,
`latitude` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idasset`),
KEY `cost_center_fk_idx` (`idcost_centre`),
KEY `iduser_fk_idx` (`iduser`),
KEY `category_name_fk_idx` (`category_name`),
CONSTRAINT `category_name_fk` FOREIGN KEY (`category_name`) REFERENCES `category` (`category_name`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `iduser_fk` FOREIGN KEY (`iduser`) REFERENCES `user` (`iduser`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=93 DEFAULT CHARSET=utf8;
If I run that exact raw query in PHPMyAdmin or MySQLWorkbench it works, but not in PHP.
If I swap around the sequence of the fields I also get the same error with:
`asset`.`asset_description`
`asset`.`latitude`,
`asset`.`longitude`,
But all the other fields are fine.
Strange thing is this query worked a few weeks ago.
Am I missing something?
I don't have any ideas.
Starting to think its a PHP or MySQL setup or something.
Any help would be greatly appreciated as time is not on my side.
Sorry silly mistake, I did point to another DB.

Dynamic table using parameter or variable in table name cakephp

I am creating a dynamic table using mysql query table name should be unique that's why i have added user id to table name.
In Controller:
$last_id = $this->User->getLastInsertID();
//$table_name = 'hello_'.$last_id.'_tutors';
// debug($table_name);
$this->User->query("CREATE TABLE IF NOT EXISTS `post_`.$last_id.`_tutors` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`user_id` int(8) NOT NULL,
`tutor_name` varchar(50) NOT NULL,
`tutor_email` varchar(50) NOT NULL,
`tutor_number` varchar(50) NOT NULL,
`tutor_gender` varchar(50) NOT NULL,
`tutor_address` varchar(100) NOT NULL,
`city_id` int(11) NOT NULL,
`area_id` int(11) NOT NULL,
`matric` varchar(200) NOT NULL,
`inter` varchar(200) NOT NULL,
`graduation` varchar(200) NOT NULL,
`masters` varchar(200) NOT NULL,
`diploma` varchar(200) NOT NULL,
`other_education` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;
");
it gave syntax error like:
Syntax error or access violation: 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 '.75._tutors ( tutor_name
varchar(50) NOT NULL, tutor_email var' at line 1
if anyone may help. Thanks in advance.
Try using
$this->User->query("CREATE TABLE IF NOT EXISTS `post_".$last_id."_tutors` (

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);

Seems to be a syntax error in my MySQL

MySQL is putting off the following 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 'CREATE TABLE IF NOT EXISTS `badips` ( `id` int(10) NOT NULL auto_increment, ' at line 2
When I run the following PHP:
if (file_exists("../login/includes/config.php")) {
$db_schema = array();
$db_schema[] = "DROP TABLE IF EXISTS `badips`;
CREATE TABLE IF NOT EXISTS `badips` (
`id` int(10) NOT NULL auto_increment,
`host` varchar(50) NOT NULL,
`ip` varchar(20) NOT NULL,
`enteredhost` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
require_once('../login/includes/config.php');
require_once('open-db.php');
echo "<h3>Creating tables...</h3>";
foreach($db_schema as $sql) {
mysql_query($sql) or die(mysql_error());
}
echo "<h3>Done!</h3>";
}
But when I run the same SQL from PHPMyAdmin, it works without any flaws. I can't figure out what the problem is. Anyone know?
mysql_query() does not support multiple queries. Quoting the PHP Manual:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
replace this:
$db_schema[] = "DROP TABLE IF EXISTS `badips`;
CREATE TABLE IF NOT EXISTS `badips` (
`id` int(10) NOT NULL auto_increment,
`host` varchar(50) NOT NULL,
`ip` varchar(20) NOT NULL,
`enteredhost` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
with
$query = "DROP TABLE IF EXISTS `badips`;
CREATE TABLE IF NOT EXISTS `badips` (
`id` int(10) NOT NULL auto_increment,
`host` varchar(50) NOT NULL,
`ip` varchar(20) NOT NULL,
`enteredhost` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26";
$db_schema = explode(";",$query);
mysql_query() does not support multiple queries., replace with
$sql="DROP TABLE IF EXISTS `badips`;";
mysql_query($sql) or die(mysql_error());
$sql="CREATE TABLE IF NOT EXISTS `badips` (`id` int(10) NOT NULL auto_increment, `host` varchar(50) NOT NULL, `ip` varchar(20) NOT NULL, `enteredhost` varchar(50) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;";
mysql_query($sql) or die(mysql_error());

Categories