MYSQL re-fetch error - mysql

I just got error from MYSQL 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 '' at line 1"
After submiting form on the page i got that messenge.
Any ideas?
Here is sql code that i used
$sql = "SELECT * FROM member ".
"WHERE termId='$term' ".
"AND year='$year' ".
"AND familyId='$familyId' order by memberId";
$rs = mysql_query($sql) or die(mysql_error());

You may need to replace single quotes in your query with backticks as follows.
$sql = "SELECT * FROM member ".
"WHERE termId=`$term` ".
"AND year=`$year` ".
"AND familyId=`$familyId` order by memberId";
$rs = mysql_query($sql) or die(mysql_error());

Your final SQL is
SELECT * FROM member_class WHERE termId='2' AND year='' AND familyId='' order by memberId
I suspect at least familyID, maybe also year are number fields, so this is a syntax error.

Always use mysql_real_escape_string to escapes special characters in a string.
$term=mysql_real_escape_string($term);
if(isset($year,$familyid,$term))
{
$myear=intval($year);
$mfamilyid=intval($familyid);
$sql = "SELECT * FROM member WHERE termId='$term'
AND year='$myear'
AND familyId='$mfamilyId'
order by memberId";
$rs = mysql_query($sql) or die(mysql_error());
}

Related

Query Failed: You have an error in your SQL syntax

Database query failed: 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 1' at line 1
function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
?>
Try to replace all the query thing by this:
$query = "
SELECT *
FROM subjects
WHERE id = $subject_id
LIMIT 1";
I'd be looking at what your passing into $subject_id.
Please please please don't use SELECT *. Even if you want all of the columns, write them out. If your tables change and get more columns added then your pulling along more data.

SQL syntax error 1064

If someone can explain why doesnt work, i would apriciate :)
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 ''categorias'' at line 1
<?php
include "includes/connection.php";
$query = "SELECT * FROM 'categorias' ";
$result = mysql_query($query) or die (mysql_error());
while ($categorias = mysql_fetch_array($result)) {
echo "<p>" . $categorias ['descricao'] . "</p>";
}
?>
Remove the single quotes from your table name. Instead use back ticks if needed.
$query = "SELECT * FROM categorias ";
Or
$query = "SELECT * FROM `categorias` ";

MySQL syntax error, for no apparent reason

I have this MySQL statement writen in PHP, but it seems to contain a syntax-error.
$user_forum_sql = ( !empty($forum_id) ) ? " WHERE session_page = " . intval($forum_id) : '';
$sql = "SELECT * FROM " . $session_table_name . '"'.$user_forum_sql.'"';
This is the error I'm having. I'm not sure what is causing it.
SQL 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 '""' at line 1
SELECT * FROM ""
$user_forum_sql = ( !empty($forum_id) ) ? " WHERE session_page = " . intval($forum_id) .'' ;
$sql = "SELECT * FROM " . $session_table_name.$user_forum_sql;
echo $sql;
Your $session_table_name is empty, so you might have not started your session or have not set the value.

SQL Like Statement with multiple WHERE clauses

I am having an issue getting this to work. I have multiple WHERE statements that need to happen based on conditional information from the search query. Within there I can't seem to get the LIKE statements to work.
In the database the STREET_NUM & STREET_NAME are in different rows. I am using one input field to check against called $address
I am also struggling with getting the MIN & MAX to work.
Here is the Query:
$sql = "SELECT * FROM arc_property_res WHERE ( arc_property_res.STATUS = 'Active'";
if(!empty($_GET['city'])){
// City only query!
$sql .= "AND arc_property_res.CITY = '{$_GET['city']}'";
}
if(!empty($_GET['neighborhood'])){
// Hood only query!
$sql .= "AND arc_property_res.SUBDIVISION = '{$_GET['neighborhood']}'";
}
if(!empty($_GET['mls-number'])){
// MLS only query!
$sql .= "AND arc_property_res.MLS_ACCT = '{$_GET['mls-number']}'";
}
if(!empty($_GET['min-price']) && !empty($_GET['max-price'])){
// MIN AND MAX only query!
$sql .= "AND arc_property_res.LIST_PRICE = MIN('{$_GET['min-price']}') MAX('{$_GET['max-price']}')";
}
if(!empty($_GET['num-of-beds'])){
// BEDS only query!
$sql .= "AND arc_property_res.BEDROOMS = '{$_GET['num-of-beds']}'";
}
if(!empty($_GET['num-of-baths'])){
// BATHS only query!
$sql .= "AND arc_property_res.BATHS_FULL = '{$_GET['num-of-baths']}'";
}
if(!empty($_GET['mls-number'])){
// BATHS only query!
$sql .= "AND arc_property_res.MLS_ACCT = '{$_GET['mls-number']}'";
}
if(!empty($_GET['address'])){
$sql .= "AND arc_property_res.STREET_NUM LIKE '%{$_GET['address']}'";
$sql .= "OR arc_property_res.STREET_NAME LIKE '{$_GET['address']}%'";
}
$sql .= ") ORDER BY {$orderby}{$price_order}{$comma}{$list_date}";
I think all you need are some parentheses around the arc_property_res.STREET_NUM. Further, I would recommend you add some spaces around each line in your entire code so that you don't get syntax errors.
if(!empty($_GET['address'])){
$sql .= " AND (arc_property_res.STREET_NUM LIKE '%{$_GET['address']}' ";
$sql .= " OR arc_property_res.STREET_NAME LIKE '{$_GET['address']}%') ";
}
In addition to the obvious "Bobby Tables" issue that your query has, the problem at hand is that you do not insert a space in front of AND. This results in queries that look like this:
AND arc_property_res.BEDROOMS =3AND arc_property_res.BATHS_FULL =2
Note that there is no space between 3 and AND - a syntax error.
You should look into parametrizing your queries, and modifying it in a way that ignores the parameters that have been set to NULL.
SELECT * FROM arc_property_res WHERE ( arc_property_res.STATUS = 'Active'
AND (arc_property_res.CITY = #cityParam OR #cityParam is NULL)
AND (arc_property_res.SUBDIVISION = #subdiv OR #subdiv is NULL)
...
)
This modification would let you keep the query the same regardless of the number of parameters that were actually set, get you the same results, taking pretty much the same time.
$sql .= "AND arc_property_res.LIST_PRICE = MIN('{$_GET['min-price']}') MAX('{$_GET['max-price']}')";
The min and max functions are for when you want to get the min and max of a field in your database.
What you want is to compare the list price to see if it falls in between the min and max values supplied by the user.
$sql .= " AND arc_property_res.LIST_PRICE >= '{$_GET['min-price']}' AND arc_property_res.LIST_PRICE <= '{$_GET['max-price']}'";

The right syntax to use near 'ORDER BY date DESC LIMIT 1' at line 4 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I keep getting the following error:
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 date DESC LIMIT 1' at line 4
function printSoldiersOfRank($rank, $branch)
{
$soldier = new soldierClass;
$sql = "SELECT * FROM soldier WHERE rank = $rank AND branch = '$branch'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)):
$soldier->getInfo($row['sID']);
$soldier->printInfo();
endwhile;
function printInfo()
{
$sql = "SELECT promoter, name, date
FROM soldier s, log l
WHERE s.sID = l.promoter AND l.promotee = $this->sid
ORDER BY date DESC LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<div class='soldierInfo'>";
echo "<a href='index.php?page=soldier&sid=$this->sid'>";
echo $this->name;
echo "</a><br />";
echo "Last Promoted: ";
echo date("d M Y", $row['date']);
echo "<br />By: <a href='index.php?page=soldier&sid=$row[0]'>";
echo $row['name'];
echo "</a></div>";
}
I'm assuming the error is in my printInfo() function. All help is appreciated, thanks in advance.
Don't assume that the error is anywhere. Instead create an error message that gives you all the information you need to debug. Here is an example.
$sql =
"
SELECT promoter, name, date
FROM soldier s, log l
WHERE ( s.sID = l.promoter AND l.promotee = $this->sid )
ORDER BY date DESC
LIMIT 1
"
;
$result = mysql_query($sql);
if (!$result)
{
echo PHP_EOL;
echo "<br/>";
echo "FAILED QUERY $sql";
echo "<br/> ON ";
echo __LINE__;
echo " IN ";
echo __FILE__;
echo "<br/>";
echo " BECAUSE ";
die(mysql_error());
}
If you use something like that, you will be able to see the query that failed and the location of the failure with certainty. As it stands now, you have a query that uses "ORDER BY date DESC" and an error message that says "ORDER BY name ASC" so it leaves me to wonder if we are looking at the right script!
Try this:
$sql = "SELECT promoter, name, date
FROM soldier s, log l
WHERE s.sID = l.promoter AND l.promotee = '".$this->sid."'
ORDER BY date DESC LIMIT 1";
Date is a variable type. Try enclosing the field named 'date' with ``
Example.
set #soldierId = 1 ;
SELECT promoter, name, `date`
FROM soldier s, log l
WHERE s.sID = l.promoter AND l.promotee = #soldierId
ORDER BY `date` DESC LIMIT 1
When you get an error message on a SQL statement from MySQL like
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 'xxxx' at line n
you have to keep an eye on your statement before 'xxxx'.
The SQL Parser stopped parsing the statement because of an error and tells you at what line this (stop) happens and what's the rest of the unparsed statement.
In this case the statement (received by MySQL) will look like this ($this->sid contains nothing)
SELECT promoter, name, date
FROM soldier s, log l
WHERE s.sID = l.promoter AND l.promotee =
ORDER BY date DESC LIMIT 1
Give it a try and you will get the same error message.
You should check if $this->sid contains something useful before the query.
BTW: Using a parameterized statement will get you out of this special pitfall too (and several others)
since u didnt say anything about your tables then
assign your columns by an aliace
$sql = "SELECT s.`promoter`, s.`name`, s.`date`
^-----------^--------------------------------if they are log table then replace by l
FROM soldier s, log l
WHERE s.sID = l.promoter AND l.promotee = '".$this->sid."'
ORDER BY s.`date` DESC LIMIT 1";
^--------------------------------look what table the date is.
i just guessed and made soldier if its log table then change where there is s to l
and make date between backtiks , because date is reserved word for mysql