What's wrong with this query? - php

I have this query, running from a PHP page:
$feed_sql = "SELECT id, title, description, rssDate
FROM feed
WHERE MATCH (title) AGAINST ('" . $rows['suburb'] . "')
AND NOT EXISTS(SELECT feed_id, recipient_id, issent
FROM tracking_table
WHERE tracking_table.feed_id = $feed_id
AND tracking_table.recipient_id = $recipient_id
AND tracking_table.issent = 'Y')
GROUP BY pubDate
ORDER BY pubDate DESC
LIMIT 1";
However, it returns the following errors upon running it:
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 'AND tracking_table.recipient_id =
AND tracki' at line 7
Line 7 being this:
AND tracking_table.recipient_id = $recipient_id
Some server information:
PHP Version 5.2.6-1+lenny9
MySQL Version 5.0.51a
Thanks :-)

As you can see here:
'AND tracking_table.recipient_id = AND tracki'
// value missing here --^
the value of $recipient_id seems to be empty and generates invalid syntax.

Perhaps $recipient_id is an empty string. Please check it

Related

PHP syntax error in SELECT mySQL query

I am trying to execute this mySQL query in PHP.
$sql = "SELECT * FROM Property
WHERE CONCAT(name, '',
contact_number , '',
hostel_address,'',
renter_name,'',
other_details,'',
date_posted,'') LIKE '%".$var."'
ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y')";
An I am getting following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y') ASC' at line 1
Any help here :(
may try this:
$sql = "SELECT * FROM Property WHERE CONCAT(name, '',contact_number , '', hostel_address,'',renter_name,'',other_details,'',date_posted,'') LIKE '%".$var."' ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y')";
Note that there should be an ending single quote before ORDER BY

WordPress database error: You have an error in your SQL syntax

Below WordPress database error is coming:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '' at line 1 for query SELECT * FROM wp_author_followers WHERE
author_id = made by require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
Here is my code.
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}author_followers WHERE author_id = $author_id", OBJECT );
$followcounter = count($results);
return $followcounter;
Everything was correct, Just Author ID was not there, I have fixed this issue. Thanks for your attention and help.
echo "SELECT * FROM {$wpdb->prefix}author_followers WHERE author_id = $author_id";
Try to echo $author_id before the SQL query, and check SQL statement after replacing $author_id with its value.
Thanks

Getting values from different tables in mysql query (practical example)

I have two tables: first and second
this and that are primary keys, common and always present on both tables, so I guess there is no need of left joins
$query = "SELECT
first.one,first.going,first.what,first.ever,second.another,second.outre,second.oneplus,second.more,second.anotherthing,second.alldifferent
WHERE second.THIS = first.THAT AND first.is = '1' AND
first.yet = '$variable' AND second.againe = '1'";
The error is the following
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 second.THIS = first.THAT AND first.is = '1' AND first.y' at line 1
But I can't get to understand why this happens.
Any help on this one? Ty very much
You need to specify a table name.
$query = "SELECT first.one, ... ,second.alldifferent WHERE ...";
Should be
$query = "SELECT first.one, ... ,second.alldifferent FROM first, second WHERE ...";

Using a PHP variable within a MySQL query string WHERE clause

What exactly is the correct syntax for adding PHP variables to a MySQL string?
This is my query:
"SELECT cd.SectionID, cd.CompanyName, cd.ShowOnSite, cd.LiveDate, cd.EndDate, cds.SiteID, s.SiteName
FROM CompanyDirectory cd
LEFT JOIN CompanyDirectorySections cds ON cd.SectionID = cds.SectionID
LEFT JOIN Sites s ON cds.SiteID = s.SiteID
WHERE s.SiteID = " . $id . " AND cd.ShowOnSite = 'y'
ORDER BY cd.EndDate DESC"
But it throws the following:
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 'AND cd.ShowOnSite = 'y' ORDER BY cd.EndDate DESC' at line 5
I have also tried WHERE s.SiteID = $id and WHERE s.SiteID = '" . $id . "' but to no avail. The former gives a blank screen, and the latter gives the aforementioned error. The variable is an integer.
I have tried the query in phpMyAdmin and it works perfectly, substituting the variable for an actual ID.
Note: if it's important, $id has been received from a form via $id = $_POST['id']; before the query, and then stripped and escaped.
Thanks.
If MySQL is saying there is an error near "AND cd.ShowOnSite = 'y'", this normally means there's an issue with whatever comes before it - in this case, the $id.
Can you print out the query in your PHP file? This may show you that $id is in fact blank, which would make the query look like "WHERE s.SiteID = AND cd.ShowOnSite = 'y'".
If it's blank, there's obviously something wrong with the $id value which you will need to sort out before your MySQL code.

Use array in where clause not working

Hi I have a query in php file which is used to filter the data in file from mysql database
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"69\"";
In this line if Reg_no = \"69\"" , if i change the 69 to any value data is being modified but if i use an array instead of 69 then its not working like this
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = " . $fc . "";
But if i use
$fc = 69;
echo $fc;
Then its working but not on that line please tell me how to code this The error on which i get is
Error
Error while accessing the database:
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 '' at line 1
select count(*) from deposit where Reg_no =
from your Reg_no =\"69\""
and your Reg_no =". $fc."";
are you not missing the "" of the $fc
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"" . $fc . "\"";
to match your 69 example.
In your original question you stated this error text
Error
Error while accessing the database:
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 '' at line 1
select count(*) from deposit where Reg_no =
If the $fc would be an array you would see this in the query as such. If i remember correctly it would look like that ...
Error
Error while accessing the database:
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 '' at line 1
select count(*) from deposit where Reg_no = Array
As it does not i assume that the variable $fc is empty. Did you check the variable or better create the query and log it somewhere to check the query as it gets sent to the sql server.
As mentioned, if it would be an array PHP would convert it when wrongly used to the text "Array" which you should find in the query.
i guess you try to do something like
"where Reg_no IN (".implode(",", $fc).")";

Categories