Getting sql error PHP - php

I am following a codeiniter tutorial, which tells me to make a database with the following code (using mysql):
$sql = "create database login;
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ";
I try to run this, using my own php code, but i keep getting this error, and have no idea how to solve it:
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 'CREATE TABLE IF NOT EXISTS `user_login` ( `id` int(11) NOT NULL AUTO_INCREM' at line 2
No idea what it could be.

create database login;
and
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
are two commands which are to be executed separately. Have the database created before you try to create the table - either via a SQL client or by executing something like:
$sql1 = 'create database login;';
$sql1->execute();
$sql2 = 'CREATE TABLE IF NOT EXISTS `user_login`...';
$sql2->execute();

Related

Auto creating table if not existing is causing error

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

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

SQL error when creating multiple tables

I'm trying to create multiple tables with a query, but it doesn't seem to work. I'm using PHP code to execute the query. Below is my PHP code, my query and my error.
My PHP code:
<?php
$conn=mysql_connect("localhost","secret","secret") or die("Kan geen verbinding maken met de DB server");
mysql_select_db("secret",$conn) or die("Kan database niet selecteren");
$query_file = 'tables.txt';
$fp = fopen($query_file, 'r');
$sql = fread($fp, filesize($query_file));
fclose($fp);
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
mysql_close($conn);
?>
My query:
CREATE TABLE pages(
id INT NOT NULL AUTO_INCREMENT,
pagid VARCHAR(32) NOT NULL,
title VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
image VARCHAR(65) NOT NULL,
youtube VARCHAR(32) NOT NULL,
primary key ( id ));
CREATE TABLE members(
id INT NOT NULL AUTO_INCREMENT,
username VARCHAR(65) NOT NULL,
password VARCHAR(65) NOT NULL,
primary key ( id ));
CREATE TABLE news(
id INT NOT NULL AUTO_INCREMENT,
pagid VARCHAR(32) NOT NULL,
title VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
image VARCHAR(150) NOT NULL,
youtube VARCHAR(32) NOT NULL,
date VARCHAR(32) NOT NULL,
primary key ( id ));
CREATE TABLE gallery(
id INT NOT NULL AUTO_INCREMENT,
image VARCHAR(65) NOT NULL,
title VARCHAR(65) NOT NULL,
description TEXT NOT NULL,
url VARCHAR(200) NOT NULL,
category VARCHAR(65) NOT NULL,
primary key ( id ));
My 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 members( id INT NOT NULL AUTO_INCREMENT, username VARCHA' at line 10
Does anyone have any idea what I'm doing wrong here?
Try this:
$q1 = "CREATE TABLE pages(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
pagid VARCHAR(32) NOT NULL,
title VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
image VARCHAR(65) NOT NULL,
youtube VARCHAR(32) NOT NULL)";
$q2 = "CREATE TABLE members(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(65) NOT NULL,
password VARCHAR(65) NOT NULL)";
mysql_query($q1);
mysql_query($q2);
If you're using mysql_query, it only supports one query at a time.
However, I would recommend you avoid using mysql_* functionality as its depreciated. Try mysqli or PDO
You're propably using mysql_query. The documentation explicitly states that multiple queries are not supported. As each of your create statements is an own query, you'll have to split up your query string.

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