Am I getting an SQL syntax error - php

CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT,
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;
INSERT INTO `contracts` (`contractId`, `username`, `title`, `contractText`, `date`, `contractState`) VALUES
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(32) CHARACTER SET utf8 NOT NULL,
`password` varchar(32) CHARACTER SET utf8 NOT NULL,
`email` varchar(20) CHARACTER SET utf8 NOT NULL,
`usergroup` tinyint(1) NOT NULL COMMENT,
`name` varchar(32) CHARACTER SET utf8 NOT NULL,
`lastname` varchar(32) CHARACTER SET utf8 NOT NULL,
`state` tinyint(4) NOT NULL COMMENT,
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci;
When running this query on the SQL server, I am getting the following error:
MySQL said: Documentation
#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 '
title varchar(20) CHARACTER SET utf8 NOT NULL,
contractText text CHAR' at line 13
why this is coming up?

You did not add a comment to the username column.
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT ,
add one here---------------------------------------^
or remove the COMMENT keyword

in this line
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
add some comment in for COMMENT or Remove Comment. Below is working code.
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'comment',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3-
panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;

Just fix your table creation to add explicit comment or remove COMMENT keyword if it is not needed.
Try modify it to become:
CREATE TABLE IF NOT EXISTS `contracts` (
`contractId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT 'add explicit comment her',
`title` varchar(20) CHARACTER SET utf8 NOT NULL,
`contractText` text CHARACTER SET utf8 NOT NULL,
`date` datetime NOT NULL,
`contractState` tinyint(1) NOT NULL COMMENT '1-Nepatvirtinta, 2- patvirtinta, 3- panaikinta, 4- atmesta',
PRIMARY KEY (`contractId`),
UNIQUE KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=15 ;

Remove comment from this line:
`username` varchar(32) CHARACTER SET utf8 NOT NULL , //COMMENT

Related

execute sql query in php doesn't work ,but the query work in phpmyadmin

i have my database sql query file and i wanna run it in php by the code below
$query = file_get_contents('./sqlquery.txt');
print $query;
$conn->query($query);
but it return this 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 `ads` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ur' at line 2
i copied the print output in to the phpmyadmin and everything work well, what's wrong here?
my sql query is the this
DROP TABLE IF EXISTS `ads`;
CREATE TABLE IF NOT EXISTS `ads` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(300) NOT NULL,
`path` varchar(200) NOT NULL,
`width` int(11) NOT NULL,
`height` int(11) NOT NULL,
`priority` int(11) NOT NULL,
`adsalter` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`adstitle` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
DROP TABLE IF EXISTS `comment`;
CREATE TABLE IF NOT EXISTS `comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(120) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`comment` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`contentid` int(11) NOT NULL,
`parentid` int(11) NOT NULL DEFAULT '0',
`date` varchar(250) NOT NULL,
`haschild` int(1) NOT NULL DEFAULT '0',
`visible` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`size` int(14) NOT NULL,
`type` varchar(50) NOT NULL,
`newsid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
DROP TABLE IF EXISTS `frgpss`;
CREATE TABLE IF NOT EXISTS `frgpss` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`ip` varchar(31) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
`token` varchar(27) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`date` int(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `news`;
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`titrimage` varchar(100) NOT NULL,
`titr` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`titralter` varchar(160) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`newsshurt` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`keywords` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`author` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`branch` int(11) NOT NULL,
`date` varchar(160) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
`visible` int(1) NOT NULL DEFAULT '0',
`visited` int(11) NOT NULL DEFAULT '0',
`titrtitle` varchar(200) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
DROP TABLE IF EXISTS `signup`;
CREATE TABLE IF NOT EXISTS `signup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`family` varchar(50) NOT NULL,
`email` varchar(80) NOT NULL,
`gender` varchar(50) NOT NULL,
`username` varchar(80) NOT NULL,
`picture` varchar(80) NOT NULL,
`password` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL,
`lastname` varchar(60) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(40) NOT NULL,
`userregistereddate` varchar(60) NOT NULL,
`key` varchar(60) NOT NULL,
`type` varchar(20) NOT NULL,
`userphoto` varchar(50) NOT NULL,
`usergroup` varchar(300) NOT NULL,
`ipaddress` text NOT NULL,
`telnumber` varchar(14) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
you're executing multiple queries here, it won't work simply, I'm pretty sure that in query function($conn->query($query);) you're using mysql_query() or mysqli_query(), but you'll have to do is to use mysqli_multi_query() instead. Have a look here at docx
$con = mysqli_connect($host, $user, $pass, $db) OR die(mysqli_error($con));
$query = file_get_contents('./sqlquery.txt');
mysqli_multi_query($con, $query);
mysqli_close($con);
Hope this will help you
Note: Assuming that all queries are working fine while executing in PHPMyAdmin.

Loading CSV file into MYSQL database not importing rows

I have the following function:
function bulk_insert_file($filename) {
$file_location = 'assets/temp/'.$filename;
$sql = 'LOAD DATA LOCAL INFILE '."'$file_location'".' INTO TABLE p4p.users_csv_import
FIELDS TERMINATED BY \',\'
LINES TERMINATED BY \'\\r\\n\'
IGNORE 1 LINES';
$this->db->query($sql);
}
And the following CSV file:
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
,2,unique_id,first_name,last_name,email,company,nonprofit,username,password,dimension_data,raw_csv_data,
However, when I run the code no data is imported. If I remove the IGNORE 1 LINES part I will at least get the first row imported.
CREATE TABLE `users_csv_import` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL DEFAULT '1',
`unique_id` varchar(255) DEFAULT '',
`first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`last_name` varchar(50) CHARACTER SET latin1 NOT NULL,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`company` int(11) NOT NULL DEFAULT '0',
`nonprofit` int(11) NOT NULL,
`username` varchar(100) CHARACTER SET latin1 NOT NULL DEFAULT '',
`password` varchar(34) CHARACTER SET latin1 NOT NULL DEFAULT '',
`dimension_data` text,
`raw_csv_data` text,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here is the specs on the table..
I assume the csv file is in unix line terminators format.
You should therefore try importing with LINES TERMINATED BY \'\\n\'.

mysql php Cannot take foreign key value

Hello I have a problem with my php code.. there are 3 tables for a recruitment system.
CREATE TABLE IF NOT EXISTS `members` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`password` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`cpassword` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`email` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`role` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`uid`)'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=60 ;
The candidate table :
CREATE TABLE IF NOT EXISTS `candidate` (
`fullname` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`webpage` varchar(150) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`tel` int(35) NOT NULL,
`nationality` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`position` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`interviewed` varchar(30) NOT NULL DEFAULT 'No',
`rating` varchar(30) NOT NULL,
`c_id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
PRIMARY KEY (`c_id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=135 ;
The academic table :
CREATE TABLE IF NOT EXISTS `academic_candidate` (
`degree` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`exp_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment1` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`proposed_positions` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`research_years` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`comment2` varchar(300) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`department` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`a_id` int(25) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
PRIMARY KEY (`a_id`),
UNIQUE KEY `id` (`a_id`),
KEY `uid` (`uid`),
KEY `c_d` (`c_id`),
KEY `c_id` (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;`
-- Constraints for table academic_candidate
ALTER TABLEacademic_candidate
ADD CONSTRAINTacademic_candidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINTacademic_candidate_ibfk_2FOREIGN KEY (c_id) REFERENCEScandidate(c_id) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table candidate
ALTER TABLEcandidate
ADD CONSTRAINTcandidate_ibfk_1FOREIGN KEY (uid) REFERENCESmembers(uid) ON DELETE CASCADE ON UPDATE CASCADE;
--
Now, I use this query in order to store the values in the table academic_candidate
session_start();
$query = "SELECT * FROM academic_candidate WHERE degree = '$degree'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count > 0){
echo "You ALready complete the form </br>";
header("Location:../candidate/candidate_index.php");
}
else{
$degree =($_POST['degree']);
$exp_years = ($_POST['exp_years']);
$comment1 = ($_POST['comment1']);
$proposed_positions = ($_POST['proposed_positions']);
$research_years=($_POST['research_years']);
$comment2=($_POST['comment2']);
$department=($_POST['department']);
$uid=($_SESSION['uid']);
$query1 = "INSERT INTO academic_candidate
(degree,exp_years,comment1,proposed_positions,research_years,comment2,department,uid,c_id)
SELECT
'$degree','$exp_years','$comment1','$proposed_positions','$research_years','$comment2','$department','$uid','$c_id'
FROM candidate
WHERE uid='$uid' AND c_id='$c_id' ";
$result = mysql_query($query1);
if(!$result){
echo "Error";
die (mysql_error());
}
else{
header("Location:../candidate/view_application.php");
}
}
My problem is that stores all the values on the table academic_candidate table but the c_id is 0 . What can I do in order to take the candidate.c_id?
You're passing literal values to your INSERT INTO statement.
Consider the difference:
SELECT 'bar' FROM foo;
SELECT bar FROM foo;
Here's a demo on SQLFiddle.
Looking at your query:
SELECT '$degree'
Is not the same as:
SELECT degree
One of these is simply echoing whatever value you pass in, the other is actually selecting a column from the DB.

Find posts of user grouped by month, unixtime

I'd like to find the number of posts for each user grouped by month.
I'm currently using INT(10) unsigned to store the date of posts.
what would be a super fast way to do this?
CREATE TABLE IF NOT EXISTS `media` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`class` tinyint(1) NOT NULL DEFAULT '1',
`date_class_changed` int(10) unsigned NOT NULL,
`title` char(5) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`url` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`media` enum('image','video') CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`thumb` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`description` varchar(140) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`date` int(10) unsigned NOT NULL,
`file` varchar(1024) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`hash` char(32) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`hashtag` text CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`meta` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
`ip` int(10) unsigned NOT NULL,
`kind` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`pid`),
UNIQUE KEY `title` (`title`),
KEY `hash` (`hash`),
KEY `class_date` (`class`,`date_class_changed`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1022724 ;
This is the table, I'm talking about, I'd like to display the number of posts for each user for each mont, such as: september 2012, User X, N posts etc..
The query I'm using after the help from #fthiella is:
SELECT
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth, username, COUNT(*) as Posts
FROM
media
WHERE username = 'foobar'
GROUP BY 1
ORDER BY 1 DESC
thanks God it's fast enough, now I'll try to optimize in case it's not using an index, but for now with almost 1M record is doing good. cheers.
SELECT
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth,
username,
COUNT(*) as Posts
FROM
media
GROUP BY
DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m') as YearMonth,
username

PHP MySQL CREATE TABLE

I'm trying to create a table using PHP, PDO and MySQL.
For the needs of my application, the name of the table has to be a variable.
Here is my code :
$request = $pdo->prepare("CREATE TABLE IF NOT EXISTS :table (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;");
$request->execute(array(
'table'=>$uuid));
Can't I use ":table" in the MySQL statement ??
Currently I wrote :
[...]
CREATE TABLE IF NOT EXISTS `$uuid`
[...]
This works but it sounds weird to me ^^' Is it the only solution to my problem ?
You can't pass the table name as parameter. If you want to create table with variable name you must use dynamic query.
$pdo->query("CREATE TABLE IF NOT EXISTS userfiles (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int unsigned NOT NULL,
`parent_id` bigint(20) unsigned NOT NULL,
`position` bigint(20) unsigned NOT NULL,
`left` bigint(20) unsigned NOT NULL,
`right` bigint(20) unsigned NOT NULL,
`level` bigint(20) unsigned NOT NULL,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8");
This is THE ONLY proper way of handling such situations.
Such matters are very basic things.
and your current setup is just like a car with square wheels.
Despite of your shortage of time you have to make it single table.
Otherwise you will waste A LOT more time and eventually will turn to the proper design anyway but after innumerable pains

Categories