SQL syntax error 1064 - mysql

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

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.

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.

MySQL Syntax Error with wpdb query/prepare

I'm simply trying to run a query using wpdb query and prepare statements.
On my page it returns:
[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 'myQuery' at line 1]
PHP Code:
<?php
$dirName1 = 'C:/wamp/www/c2c/wp-content/themes/flawless-v1-01';
$dirName2 = 'C:/wamp/www/c2c';
require_once($dirName1.'/config/setup.php');
require_once($dirName2.'/wp-config.php');
require_once($dirName2.'/wp-load.php');
$wpdb->show_errors();
$tableName = $wpdb->prefix . "user_orders";
$user = wp_get_current_user();
$userId = $user->ID;
$userName = $user->user_login;
// echo $tableName . ': ' . $userId . ': '. $userName;
// echo var_dump($tableName);
// echo var_dump($userId);
// echo var_dump($userName);
// These echo the correct formats for the prepare statement below
$myQuery = $wpdb->query(
$wpdb->prepare("
SELECT *
FROM $tableName
WHERE `user_id` = %d
AND `user_name` = %s",
$tableName,
$userId,
$userName)
);
$results = $wpdb->get_results(myQuery, ARRAY_A);
?>
You're missing the dollar sign before your variable name:
$results = $wpdb->get_results(myQuery, ARRAY_A);
should be
$results = $wpdb->get_results($myQuery, ARRAY_A);

SQL statement not working SELECT 1064 Error in your SQL syntax

My SQL statement doesn't work...
Here is my variable:
$email="test#test.com";
These statements doesn't work :
$sql = "SELECT * FROM table WHERE email = $email";
$sql = 'SELECT * FROM table WHERE email = ' . $email;
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 '#test.com' at line 1
But if I use a string instead of the variable, it works:
$sql = "SELECT * FROM table WHERE email = 'test#test.com'";
What's wrong with my statement?
Thanks!
please escape strings in mysql
$sql = "SELECT * FROM table WHERE email = $email";
$sql = 'SELECT * FROM table WHERE email = ' . $email;
This should work
$sql = "SELECT * FROM table WHERE email = '$email'";
table is a keyword, so can write like this
$sql = "SELECT * FROM `table` WHERE email='$email'";
$this->db->where('email', $this->input->post('email'));
$query =$this->db->get($this->user);
//$sql = "SELECT * FROM user WHERE email = '$email'";
if($query->num_rows == 1)
{
return true;
}
else
{
return false;
}
}
}

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