I'm getting the error: Incorrect syntax near the keyword 'AS'.
I'm trying to select a column from my MSSQL database while NOT SELECTING the first character of that field.
Below is the code (Within PHP):
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) FROM Table WHERE [Column] ='".$search."' AS Column2";
First of all. Please be careful. Your code looks vulnerable to an SQL injection. Better use paramterized queries.
The second "AS" at the end is not needed.
Try it that way:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS Revlv2 FROM table_name WHERE [Objkt] ='".$search."'
Or better yet:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS column_name FROM table_name WHERE [Objkt] = ?
Read more about parameterized queries here
Also take care when the Revlv column only contains an empty string. The query will fail in that case.
try this:
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
As all answers simply fix the syntax instead of fixing the logic:
There's no need to use RIGHT + LEN to extract everything but the first character, simply use
substring(Revlv from 2) AS Revlv2 -- Standard SQL to extract everything after the first character
As SQL Server has a slighty different syntax:
substring(Revlv, 2, 8000) AS Revlv2 -- T-SQL to extract everything after the first character
You need to put AS after the SELECT part of your query never put it at the end of your query. Try to do it like this
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
SOURCE
try this.
$sql="SELECT RIGHT(Revlv, LEN(Revlv)-1) AS Revlv2
FROM tablename where['objkt']='$search'";
I would like to combine two columns in one column as Fullname, and for that I have written the following code:
$this->db->select('CONCAT(first_name," ",last_name) AS FullName');
$this->db->from('customer');
$this->db->where('user_id',1);
$query = $this->db->get();
return $query->result_array();
The resulting query would be:
SELECT CONCAT(first_name," ",last_name) AS FullName
FROM customer
WHERE user_id = 1
but when i execute the above code it gives me:
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 'FROM (customer) WHERE user_id = '1' at line 2
I have also tried with concat_ws group_concat but not able to get it work. Can anyone see what I'm doing wrong?
By default, CI tries to escape what you pass to db->select() (in case you were passing in user-generated values). You can disable this feature by passing false as a second argument.
$this->db->select('CONCAT(first_name," ",last_name) AS FullName', false);
I have been through this before with CI, in my case CI was wrongly creating the query, for example putting simgle quotes where they shouldn't be.
My advice create the query yourself and run it, you could be surprise :P
when i run my php page this error show:
Unable to run query:Unknown column 'NUM_OPTIONAL_COLLEGE_COURSE' in 'field list'
$sql ="INSERT INTO MAJOR (`COL_NO`, `DEPT_NO`, `YEAR`, `NUM_OPTIONAL_UNI_COURSE`,
`NUM_MANDATORY_UNI_COURSE`, `NUM_OPTIONAL_COLLEGE_COURSE`,
`NUM_MANDATORY_COLLEGE_COURSE`, `NUM_OPTIONAL_DEPT_COURSE`, `NUM_MANDATORY_DEPT_COURSE`,
`NUM_FREE_COURSE`, `NUM_CUSHIONS_COURSE`, `NUM_OF_CREDIT`)
VALUES('$myq1','$myq2','$myq3','$myq4','$myq5','$myq6','$myq7','$myq8','$myq9',
'$myq10','$myq11','$myq12')";
can you help me in this ?
There is no column named NUM_OPTIONAL_COLLEGE_COURSE in table MAJOR. Re-check your column name, most probably its a spelling mistake.
The Error says it all
Verify NUM_OPTIONAL_COLLEGE_COURSE is the column name in the schema
Most errors occur if you have a column like " NUM_OPTIONAL_COLLEGE_COURSE" (mind the space in front) - use your Database Management System and Copy the name of the column.
Its a database schema error, The error means the column does not exist in table. You have to check out it carefully....You should also check, may be you are connected with wrong database. Once I also faced this type error, and my idiocy , I was runing my query on wrong database.
I have dumped the contents of a database in an sql file in a form like
insert into `a` values
(17,11,5),
(18,12,7),
(19,12,10),
(21,14,45),
(22,15,46),
(24,16,46),
(25,16,49),
(26,17,21),
(27,17,30),
(28,17,45),
(29,17,54),
(30,18,32),
(31,18,35),
(32,19,23),
(33,19,27),
(34,19,54),
(35,20,53),
(36,21,32),
(37,21,35),
(38,21,45),
(39,22,23),
(40,22,30),
(41,22,45),
(57,24,19),
(58,25,46),
(59,26,39),
(60,27,49),
(61,27,56),
(62,28,34);
insert into `b` values (14,'2009-01-06',''),
(15,'2009-02-01',''),
(16,'2009-03-01',''),
(17,'2009-03-25',''),
(18,'2009-04-05',''),
(19,'2009-04-17',''),
(20,'2009-04-18',''),
(21,'2009-04-19',''),
(22,'2009-04-23',''),
(24,'2009-07-05',''),
(25,'2009-08-02',''),
(26,'2009-08-07',''),
(27,'2009-09-06',''),
(28,'2009-09-14','');
etc..
I have 4 such tables with no foreigh key constrains. Then I try to upload the data into the db (mysql). I read the file's contents, I pass each table's insertion into an array and then i do mysql_query for each element :
$sqlArray = explode(';',$sqlFile);
for($i=0;$i<sizeof($sqlArray);$i++){
mysql_query($sqlArray[$i]) or die ('Error: '.mysql_error());;
}
The result is that the last three tables are inserted but the first one is not, and the error is :
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 ' insert into `a` values (17,11,5), (18,12,7), (19,12,10), (21,14,4' at line 1
I validated that the $sqlArray has the correct contains and the queries are correct and runnable from phpmyadmin.
The problem seems to be regardless of the first table (i.e. it will show up even if b was first) and it always seems to "cut" the query in the middle (or after almost 70 characters).
Any help will be appreciated!
Your second statement has a typo ("int" should be "into"):
insert int `b` values (14,'2009-01-06',''),
Alternately, if that's not the issue, try using separate insert statements to get a clearer error message:
insert into `a` values (17,11,5);
insert into `a` values (118,12,7);
...
I have experienced on some older versions of MySQL that they don't like the extended inserts. You can also try specifying the column names explicitly with the real names of your columns. This would be useful if you have more than 3 columns in those tables (an auto-increment column for example).
insert into `a` (`[column1name]`, `[column2name]`, `[column3name]`) values (17,11,5);
I am trying to use UPDATE with my MySQL-Database. I use the following SQL code:
$sql = "UPDATE ToDo
SET Checked = -1
WHERE Index = 1";
When I use this code i get the following error message: "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 'Index = 1' at line 3"
But when I use
$sql = "UPDATE ToDo
SET Checked = -1
WHERE Text = 'asdf'";
Everything works.
My database has one table named "ToDo" with 3 collumns: Index(int, primary key, auto_increment), Checked(bool) and Text(text).
Can't you "WHERE" a primary key or did i forget something else?
Hope you can help me.
Try adding the backticks:
UPDATE ToDo
SET `Checked` = -1
WHERE `Index` = 1";
Index is a reserved word :
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
index is a reserved word for MySQL. You need to esacpe the name by adding backticks like this:
$sql = "UPDATE ToDo
SET Checked = -1
WHERE `Index` = 'asdf'";
In order to make sure MySQL understands that you are talking about a column name and not the reserved word, you can always address the column name tablename.columnname.
In SELECT queries also using shortcuts is possible:
UPDATE ToDo SET ToDo.Checked = -1 WHERE ToDo.Index = 1
SELECT u.Index FROM users u
Also I would recommend not to use camel cases in tables and columns. This proved to be a source for errors and has no real benefit most of the time.
Boolean is 0 or 1, not -1. Try using 0 or 1 for Checked and let us know what happens.