MySQL syntax error, for no apparent reason - mysql

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.

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

Error in Line 1 but not sure

$user_forum_sql = (!empty($forum_id)) ? " WHERE session_page = " .intval($forum_id) : '';
$sql = "SELECT * FROM " . $session_table_name . $user_forum_sql;`
Don't use SELECT * - query only needed columns
Use prepared statements
Post the whole error message (mysql_error)
Print the whole sql statement and run in manually against the server
mysql queries in php should not end with a semicolon
And get the php syntax right:
$user_forum_sql = (!empty($forum_id)) ? " WHERE session_page = " . intval($forum_id) : '';
$sql = "SELECT col1, col2, col3 FROM " . $session_table_name . $user_forum_sql;

MySQL server version for the right syntax to use near

I am getting the following error:
"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 ' `DeweyEdition`
BLOB NOT NULL, , `DeweyNumber` BLOB NOT NULL)' at line 1
<br />Error No: 1064<br />ALTER TABLE tt_hj_import ADD COLUMN
(`CountryOfPub` BLOB NOT NULL, , `DeweyEdition` BLOB NOT NULL, ,
`DeweyNumber` BLOB NOT NULL) in /var/www/system/database/mysql.php on line 50"
My code:
public function alterImportTable($new_fields) {
if (!empty($new_fields)) {
$sql = "ALTER TABLE " . DB_PREFIX . "hj_import ADD COLUMN ";
$fields_sql = array();
foreach ($new_fields as $field) {
$fields_sql[] = '`' . $field . "` BLOB NOT NULL, ";
}
$sql .= '(' . implode(', ', $fields_sql) . ')';
$sql = str_replace(', )', ')', $sql);
$this->db->query($sql);
}
}
How do I fix this?
I think there is an extra space in the query, try removing , after NULL, like this:
$fields_sql[] = '`' . $field . "` BLOB NOT NULL ";
You need one ADD COLUMN per column.
I had the same issue in my program, i just change the query version
mysql_query($sql,$con)
TO
mysqli_query($sql, $con)

MYSQL re-fetch error

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());
}