SQL query: Query browser shows error 1064 - php

I cant find what happened to the MYSQL query. It was working fine and today it is throwing 1064 error can anyone help me.
In query browser i am getting 1064 error
and in mysqli_error i am getting "Unknown column 's.firstname' in 'field list'".
query ::
SELECT s.recid userId, s.firstname firstName, s.lastname lastName, s.email email, s.status status
FROM usertable s
WHERE s.email = 'xyz#gmail.com'
AND s.mobile = 'XXXXXXXX'
AND (s.enddate IS NULL OR s.enddate = '0000-00-00' OR s.enddate > '2015-04-08 11:13:34')
Error :
Unknown column 's.firstname' in 'field list'

Check the firstname spelling, white spaces on start and end of the field, if its okay, then remove firstname from query and then try it once.
This will help us to debug.

Check your table has the column firstname
Use below command to check it within query browser
DESC usertable;

Try to wrap everything with backticks (or at least column name called status) and see what happens...
SELECT `s`.`recid` `userId`, `s`.`firstname` `firstName`, `s`.`lastname` `lastName`, `s`.`email` `email`, `s`.`status` `status`
FROM `usertable` `s`
WHERE `s`.`email` = 'xyz#gmail.com'
AND `s`.`mobile` = 'XXXXXXXX'
AND (`s`.`enddate` IS NULL OR `s`.`enddate` = '0000-00-00' OR `s`.`enddate` > '2015-04-08 11:13:34')
Note that you are using status as column name, but even if it isn't declared as keyword it is used in queries like SHOW STATUS...So I will avoid that and change its name to something else. Also using status like this in some versions of phpMyAdmin produced some unexpected results. But when you wrap it with backticks you can use it as column name.

Related

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

Am pretty new to PHP and stuch with this problem.
Am trying to insert some info from a dropdown menu into a database but keep getting this error. Please help if you can.
The php code is:
<select name="group_ID">
<?php
$pos_query ="SELECT groupName, group_ID FROM 'operations' JOIN members WHERE operations.group_ID = members.group_ID";
$pos_results = $db->query($pos_query);
for ( $i=0; $i < $pos_results->num_rows ; $i++ )
{
$pos_row = $pos_results->fetch_assoc();
echo'<option value"'.$pos_row['group_ID'].'">';
echo $pos_row['groupName'].'</option>';
}
?></select></td>
I have a table called operations and trying to join it to another table called members and insert the data into the members table.
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 ''') SELECT 'Church Elders' FROM operations JOIN members ON ope' at line 1.
Sorry guys I posted the wrong code.
The actual code is:
$query = "INSERT INTO members(email, name, gender, dob, profile, password,)
SELECT $group_ID
FROM operations
JOIN members
WHERE operations.group_ID = '$group_ID'" ;
$result = $db->query($query);
Am trying to insert email, name, gender... group_ID into the members table and getting the group_ID from the operations table and gets the following error message:
Error Inserting Details. 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 ') SELECT Church Elders FROM operations JOIN members WHERE oper' at line 1.
Please help!!!!
No need to use single quote with table name('operations') & specify table name with row like groupName.operations, group_ID.operations.
You probably meant to use backticks in your SQL query, not single-quotes. Also, you probably meant the ON statement, rather than WHERE. WHERE is for filtering, while ON tells the JOIN statement which columns to join on.
SELECT `groupName`, `group_ID` FROM `operations` JOIN `members` ON `operations`.`group_ID` = `members`.`group_ID`

MySQL query errors with "You have an error in your SQL syntax"

I'm performing the following query and it seems I have an issue right where I'm doing the convert(aes_decrypt) part as the query was working fine before I added it in. I know it's something small but can't quite seem to get the statement correct.
Missing quotes or wrong quotes? I just can't see it..
$db->query("SELECT
`users`.`name` AS `users`.`name` ,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
The exact error I'm receiving 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 '.name ,convert(aes_decrypt(users.email,MySecretSa' at line
1
Alias names are user defined. Qualifiers on them is meaning less and not supported.
select users.column_name as users.column_name
from users -- this syntax is not correct
If you want just the same name as column name, omit alias name.
select column_name from table_name
And if you want explicit column/expression alias names don't use table qualifiers.
select column_name as alias_name
, col1 * col2 as expression_alias
from table_name
Yes ,it's strange: users.name AS users.name
The error in the second users.name is wrong.
can make it like this
$db->query("SELECT
`users`.`name` AS `user_name` ,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
Or you can eliminate like this
$db->query("SELECT
`users`.`name`,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
Try this sql query:-
$db->query("SELECT
`users`.`name` AS name ,
CONVERT( aes_decrypt(`users`.`email`, '$salt') USING utf8) AS email ,
`users`.`address` AS address
FROM `users`");

SQLSTATE[42000]: Syntax error or access violation:

I've been trying to fix this SQL state error from the past 30 mins.
What am I doing wrong here?
Any suggestions are welcome.
$statement=$conn->prepare("SELECT to.ipaddress as ipaddress, to.email as email, to.orderdate as orderdate, to.is_completed as is_completed, p.price as price from temporder to, products p WHERE to.productid = p.productid AND to.ipaddress=? AND to.orderdate=? ");
$statement->execute(array(
$_SERVER['REMOTE_ADDR'],
date('Y-m-d')
));
$statement->setFetchMode(PDO::FETCH_ASSOC);
Complete error
SQLSTATE[42000]: 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 'to, products p WHERE to.productid = p.productid AND to.ipaddress='::1' AND to.or' at line 1
TO (which you are using as an ALIAS) is a reserved word in MySQL , you need to surround it in backticks.
From a part of your query...
e as price from temporder to, products p WHERE to
^^^^ //<----- That's a reserved word. !
Wrap them under backtick like below..
e as price from temporder `to`, products p WHERE to
to is a reserved key word
so u need to wrap it within backtics as
`to`
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

MySQL: error when query returns no results?

I have a query that looks like this:
SELECT number
FROM table1
INNER JOIN table2
WHERE name = 'stack_overflow' AND table1.id = table2.id
AND user_id = 5
This returns a number. It does the right thing, but when inside name I pass a name that does not exist in db, PHP gives me an error. This is how I am executing it:
$stmt = $this->db->prepare($sql);
$stmt->execute();
$x = $stmt->fetchColumn();
I always get the correct $x value when the name exists in the table, however when it doesn't, I get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: 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 'name_that_is_not_in_table'
No idea....
Try to pass name in this way:
SELECT number
FROM table1
INNER JOIN table2
WHERE name = ? AND table1.id = table2.id
AND user_id = 5
$stmt = $this->db->prepare($sql);
$stmt->execute(array($name));
$x = $stmt->fetchColumn();
Sounds like you need to check how many rows are returned first... if none, notify the user. If it DOES have a returned row count, then get the name column as you are expecting.
Additionally, you should clarify the alias.column in your queries as anyone new doesn't have to guess which table a given column comes from... Such as your user_id and name columns. (and "name" might be a reserved word and cause the choke... so you might want to wrap it in tick marks
`name`

MYSQL syntax error

Every time I try to run this MYSQL statement in phpMyAdmin I get this sytax error:
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 'comments,last_comment, '%d/%m/%Y %H:%i:%s') ASlast_comment
FROMposts' at line 7
Code:
"SELECT
`posts`.`post_id` AS `id`,
`posts`.`post_title` AS `title`,
LEFT(`posts`.`post_body`, 512) AS `preview`,
`posts`.`post_user` AS `user`,
DATE_FORMAT(`posts`.`post_date`,'%d/%m/%Y %H:%i:%s') AS `date`,
`comments`.`total_comments`,
DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
FROM `posts`
LEFT JOIN (
SELECT
`post_id`,
COUNT(`comment_id`) AS `total_comments`,
MAX(`comment_date`) AS `last_comment`
FROM `comments`
GROUP BY `post_id`
) AS `comments`
ON `posts`.`post_id` = `comments`.`post_id`
ORDER BY `posts`.`post_date` DESC";
Also, all of the tables are named correctly. So, that can be ruled out.
UPDATE:
Awesome, thanks. I added the quote on comments and the extra perimeter was meant to be a period not a comma.
DATE_FORMAT() accepts two parameters, not three. See the manual.
I suppose, you meant
`comments`.`total_comments`
instead?
`comments`,`last_comment`
should be
`comments`.`last_comment`
I.e. the comma should be a dot, otherwise it looks like you are trying to pass three parameters to DATE_FORMAT.

Categories