PHP MYSQL Variable returns error - php

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";

Related

How to properly use variable in a query? postgresql

$result = pg_query(Postgres::getInstance(), "SELECT
date_start,
date_end,
cnt_hands,
cnt_hands_won,
amt_won,
id_session,
id_player,
amt_won
FROM
cash_table_session_summary
WHERE
date_start = '2016-04-27 07:20:47'");
This works perfectly.
echo $sessionStart;
$result = pg_query(Postgres::getInstance(), "SELECT
date_start,
date_end,
cnt_hands,
cnt_hands_won,
amt_won,
id_session,
id_player,
amt_won
FROM
cash_table_session_summary
WHERE
date_start = $sessionStart");
This throws this:
2016-04-27 07:20:47 Warning: pg_query(): Query failed: ERROR: syntax
error at or near "07" LINE 13: ... date_start = 2016-04-27 07:20:47 ^
in /home/haris/public_html/project/DAL_General.php on line 102
Is colon a problem? Do I need to escape it somehow? If so, how? I've google but found nothing about escaping colons.
Your date must be inside quotes. You need to change
FROM
cash_table_session_summary
WHERE
date_start = $sessionStart
To
FROM
cash_table_session_summary
WHERE
date_start = '$sessionStart'// add quotes here
For more understand about quotes check When to use single quotes, double quotes, and backticks in MySQL
I had the same issue when I was learning database work, but you need the query to be made in ""'s - Then when referencing a variable or data you need to put it in ''s, example:
$test_query = type_of_connection_query("SELECT * FROM users WHERE id = '1'");
That would not work like:
$text_query = type_of_connection_query("SELECT * FROM users WHERE id = 1");
And of course there are some times when you don't need quotes, like referencing LIMIT's
$text_query = type_of_connection_query("SELECT * FROM users WHERE id = '1' LIMIT 1");

Getting error while executing SQL query to get result search by keyword using PHP and MySQL

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 ");

MySQL,PHP: ORDER BY shoots an error.

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";

Where do I put DESC in a mysql query? (PHP)

This is my query:
$query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage";
I'm trying to sort the results by the latest (latest on top), so I tried to add DESC.
But where ever I put it in the query it gives me an error, the only way it doesn't give an error is like this:
$query = "SELECT * FROM movies ORDER BY 'timestamp' DESC LIMIT $startAt, $perPage";
But even though it doesn't give an error, it still doesn't work.
This probably seems like a silly thing to ask but it's really driving me crazy
Timestamp is the datatype in MySQL. So, always try to avoid such column name in your table. Even, if you have a column named as timestamp, you must have to use backtick. So, use below query:
$query = "SELECT * FROM table ORDER BY `timestamp` LIMIT $startAt, $perPage";
Try backtick on timestamp :
$query = "SELECT * FROM movies ORDER BY `timestamp` DESC LIMIT $startAt, $perPage";
Using backticks permits you to use alternative characters
I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.
Do not use quote in field name. Try this:
$query = "SELECT * FROM movies ORDER BY timestamp DESC LIMIT $startAt, $perPage";

Right syntax to use near order by id desc limit 1

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

Categories