Whenever I try to use ORDER BY I'm getting an error saying that it's something bad with syntax
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 'ORDER BY id' at line 1
For me, the syntax looks perfectly good, everything works as long as I don't use ORDER BY id. This is how it works:
$sql = "SELECT * FROM lessons $limit";
This is how I want it to work:
$sql = "SELECT * FROM lessons $limit ORDER BY id";
I have also tried this, but with no luck:
$sql = "SELECT * FROM lessons $limit ORDER BY id DESC";
ORDER BY needs to go before LIMIT.
Try:
$sql = "SELECT * FROM lessons ORDER BY id $limit";
Related
How Do I Concatenate PHP variable in SQL Query Then Concatenate Additional SQL Queries
$query = 'SELECT * FROM news_posts WHERE status ="approved" AND user_id='.$user_id.'ORDER BY news_id DESC';
I got an error:
check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BY news_id DESC' at line 132
$query = 'SELECT * FROM news_posts WHERE status ="approved" AND
user_id='.$user_id.' ORDER BY news_id DESC';
try add a space before "ORDER"
Simple question, but my searching is not finding an answer. I am trying to use a variable in a MySQL search and I keep getting an error. I know the variable value is good because when I pass it directly it works. Here is the error I get
"Error: something went wrong: 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 'Bank of CanadaGROUP BY YEAR(date), MONTH(date) DESC' at line "
$query = "SELECT * FROM current_rates WHERE
financial_institution =". $lenderName .
"GROUP BY YEAR(date), MONTH(date) DESC";
Strings in MySQL must be enclosed with single quotes.
Otherwise, they will be considered as reserved words/column names.
The corrected SQL should be:
$query = "SELECT * FROM current_rates WHERE
financial_institution ='". $lenderName .
"' GROUP BY YEAR(date), MONTH(date) DESC";
Also, as per answer from Bhaumik Mehta, you need to add a space before GROUP BY tag.
Try this query:
1) You need to enclose $lenderName in '' as the it is a string value.
2) You need to have space before GROUP BY keyword
$query = "SELECT * FROM current_rates WHERE financial_institution ='". $lenderName ."' GROUP BY YEAR(date), MONTH(date) DESC";
please given one space between name and group
$query = "SELECT * FROM current_rates WHERE financial_institution ='".$lenderName."' GROUP BY YEAR(date), MONTH(date) DESC";
My requirement is when user will type letter inside text box at front end it will auto search from database and give the result accordingly. I have written some query but it gave me the following error.
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 '%P%) ORDER BY member_id DESC LIMIT 0, 30' at line 1
My code is given below.
$searchKey=$_GET['searchKey'];
$keyword = '%'.$searchKey.'%';
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE (:keyword) ORDER BY member_id DESC ");
My first search keyword was p.
You want
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '$keyword' ORDER BY member_id DESC ");
or also you could do
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '" . $keyword . "' ORDER BY member_id DESC ");
(:keyword) is not going to pull in the variable for your keyword into the SQL syntax and also (:keyword) is not valid mysql
Another approach,
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '".$keyword."' ORDER BY member_id DESC ");
It's better to use single quotes inside double quotes in relevant places when executing SQL queries.Sometimes you'll need to put single quotes for table name as well.Like this,
$sql =mysqli_query($connect,"SELECT * FROM `db_restaurant_basic` WHERE rest_name LIKE '".$keyword."' ORDER BY member_id DESC ");
I spent quite a bit of time researching, could not get it resolve. Hence seeking expert advice.
Exact Error:
Error fetching Data: 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 ''LIMIT 0,10''
Here is my SQL :
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
try
{
$sql2 = 'SELECT CASE_JOB_TITLE, STATUS, CASE_WAGE
FROM EMPLOYEE
WHERE LCA_CASE_EMPLOYER_NAME like "%MICROSOFT CORPORATION%"
ORDER BY LCA_CASE_NUMBER ASC :limit';
$sth2 =$pdo->prepare($sql2);
$sth2->bindParam(':limit',$limit );
$result2 = $sth2->execute();
}
Where am I going wrong? If I use the same query in SQL Editor it works fine.
EDIT: Didn't see your first part. Try this:
It should look like:
$limit = ($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
$sql2 = 'SELECT CASE_JOB_TITLE, STATUS, CASE_WAGE
FROM EMPLOYEE
WHERE LCA_CASE_EMPLOYER_NAME like "%MICROSOFT CORPORATION%"
ORDER BY LCA_CASE_NUMBER ASC LIMIT :limit';
I solved it by removing the bind with string. I used the final set of code is ORDER BY LCA_CASE_NUMBER ASC LIMIT :limitFrom,40'; $sth2 =$pdo->prepare($sql2); $sth2->bindParam(':limitFrom –
If the code is executed, it shows the error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY ID DESC limit 1'
Like in the following example:
$prevquery = "SELECT * FROM $tbl_name WHERE ID < $ID ORDER BY ID DESC limit 1";
$prevresult= mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_row($prevresult))
{
displaying the previous ID:-
$prevID = $prevrow['ID'];
}
What should one do to prevent this?
Try this
$prevquery = "SELECT * FROM $tbl_name WHERE ID < '$ID' ORDER BY ID DESC limit 1";
If this does not work then echo the query and run in mysql phpmyadmin panel
echo $prevquery = "SELECT * FROM $tbl_name WHERE ID < '$ID' ORDER BY ID DESC limit 1";
This should work
$prevquery = "SELECT * FROM $tbl_name WHERE ID < '$ID' ORDER BY ID DESC limit 1";
$prevresult= mysql_query($prevquery) or die(mysql_error());
while($prevrow = mysql_fetch_row($prevresult))
{
displaying the previous ID:-
$prevID = $prevrow['ID'];
}
An additional note, whenever I see a generic error of the form...
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'ORDER BY ID DESC limit 1'
The hint to look "near" almost always points to a problem in the sql statement just prior to where the single-quoted string shown in the error message begins.
So in this case the error quotes your sql string beginning at 'ORDER BY' which is immediately preceded by $ID. I would bet $ID is either not defined (and ends up being blank in your sql string), or $ID is not the correct data type.
Either way, as others have suggested you need to echo or log your sql string to see what is actually being queried.
You cant user 'where' with LIMIT. Instead use ORDER by id desc LIMIT 1