I can't get a variable to work in SQL statement. I can get it to work when I replace (username = $user) with (ID = 11) which is another column from database and a specific row (11), but I want to include a specific row matching $user from column 'username', along with other random results with a limit of $sn.
When using var_dump($user) I know that the variable has a value, but can't see why it doesn't work in SQL statement.
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM profile_images
WHERE approved='N'
ORDER BY (username = $user) DESC, RAND()
LIMIT $sn)
as A ORDER BY RAND()");
Getting 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 '#googlemail.com) DESC, RAND() LIMIT 9) as A ORDER BY RAND()' at line 4
Any help appreciated.
Assuming $sn holds integer value and don't require escaping,
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM profile_images
WHERE approved='N'
ORDER BY (username = '".mysql_real_escape_string($user)."') DESC, RAND()
LIMIT $sn)
as A ORDER BY RAND()");
In general, consider using PDO and bind parameters.
Related
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'm attempting to run the following SQL statement, but I can't quite get it right.
This is what works on the iSeries within STRSQL, and also works within my PHP program. But I don't see the (mbmrxl * mbndtr) result as I do on the iSeries.
That column is blank.
SELECT SURNME, ODLBNM, ODOBNM, MBMXRL, MBNRCD, MBNDTR, (mbmxrl * mbndtr), objrnk
FROM mytable WHERE surnme = 'STP_ROLL' ORDER BY (mbmxrl * mbndtr) desc
I need to perform this calculation and place it in a new field called TOTRANK:
MBMRXL * MBNDTR
This is the php query I'm trying:
$query = "SELECT SURNME, ODLBNM, ODOBNM, MBMXRL, MBNRCD, MBNDTR, TOTRANK, objrnk FROM (select mbmrxl * mbndtr as TOTRANK, from mytable)
WHERE surnme = 'STP_ROLL' ORDER BY TOTRANK desc";
When run the above code, I get this message:
SQL statement failed Token . was not valid. Valid tokens: , FROM INTO.
SQLCODE=-104
How do I debug this?
SELECT SURNME, ODLBNM, ODOBNM, MBMXRL, MBNRCD, MBNDTR, objrnk
, mbmrxl * mbndtr as TOTRANK
FROM mytable
WHERE surnme = 'STP_ROLL'
ORDER BY mbmrxl * mbndtr desc
Your SQL select statement should be changed to the above.
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
This question already has answers here:
How can I write SQL for a table that shares the same name as a protected keyword in MySql? [duplicate]
(3 answers)
Closed 9 years ago.
I get a error message saying:
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 'key='12345' AND id='98765' LIMIT 1' at line 1
My code is:
$key = '12345';
$id = '98765';
include realpath('./inc/config.php');
$query = mysql_query("SELECT * FROM users WHERE key='{$key}' AND id='{$id}' LIMIT 1", $config) or die(mysql_error());
$result = mysql_fetch_assoc($query);
Now can anyone tell me whats wrong in this?
key is a reserved word, you need to properly quote it with backticks if you want to use it as a field name.
SELECT * FROM users WHERE `key`='{$key}' AND id='{$id}' LIMIT 1
SELECT * FROM users WHERE `key`='{$key}' AND id='{$id}' LIMIT 1
key is a reserved word
Your key and id are obviously numeric. Although adding quotes wouldn't hurt, you definitely don't need them. You edon't need brackets in any query period.
Try this:
$query = mysql_query("SELECT * FROM users WHERE key=$key AND id=$id LIMIT 1", $config) or die(mysql_error());
$result = mysql_fetch_assoc($query);
If that doesn't work just run this using PHPMyAdmin or whatever you use to run queries on your db.
SELECT * FROM users WHERE key=12345 AND id=98765
I also don't see why you would need LIMIT clause. It wouldn't break anything but if your id is actually row id it should give you a unique record.
i have the following statement
$result = mysql_query("SELECT * from rests ORDER BY name asc WHERE flag = '1' LIMIT 0 , 20");
am getting the following error
Invalid query: 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 'WHERE flag = '1' LIMIT 0 , 20' at line 1
am not sure where am going wrong :(
$result = mysql_query("SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20");
You cannot have the ORDER BY clause before WHERE clause.
Refer to the MySQL select syntax for the correct order of various clauses.
Try this:
SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20
Also Ordering in ascending is default, you may drop the asc.
The ORDER BY clause must be after the WHERE clause. That's all it is.
ORDER BY comes after the WHERE and LIMIT at the end.
WHERE goes before ORDER BY.