Have trouble with mySQL query - php

I'm am working on a project and need to use the below query statement, unfortunately my table and column names have dashes. Does anyone know how to get this to work?
SELECT * FROM 'default-table' WHERE 'ds-avail'='Yes';
Here is the error I get.
Invalid query: 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 '; SELECT * FROM [default-table] WHERE [ds-avail]='Yes'' at line

Try this
SELECT * FROM `default-table` WHERE `ds-avail` = 'Yes';
You used 'table' while you should have it like this table

SELECT * FROM default-table WHERE ds-avail='Yes';
Be sure your table is named exactly "default-table" (without quotes)
Be sure the field that you're looking for is called "ds-avail" (without quotes) and it exists in that table.

You should put the table and the column without quotes:
SELECT * FROM default-table WHERE ds-avail='Yes';

Related

MySQL SELECT record from database.table (database name contain '-')

I would like to get rows from another database so I created query:
SELECT * FROM database-test.users
MySQL result that error:
Database_Exception [ 42000 ]: SQLSTATE[42000]: Syntax error or access violation: 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.users' at line 1
How to solve this?
Thanks for reply
You need to do it like below (use back-ticks around table name):-
SELECT * FROM `database-test`.users
Or
SELECT * FROM `database-test`.`users`
I would recommend to 'back tick' all database and table names in your query. It will tell the database's SQL parser to ignore any special characters such as "-" and consider them as part of the name.
Example:
SELECT * FROM `database-test`.`users`
Try
SELECT * FROM `database-test`.users
As you can see, I have used the ` character to encapsulate database-test, which makes sure that non alpha-numeric characters, like - will be accepted in the name.

MySQL - how to match JSON string in where condition?

I am using MySQL 5.6 , one of my table field contains JSON data. I am getting syntax error when using below query -
SELECT * FROM products WHERE device_id = '1212'and product_id = '54'and option = '"{"229":"20"}"'
field option has value as {"229":"20"} I am getting following 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 'option = '"{"229":"20"}"'
LIMIT 0, 25' at line 1
Please suggest any solution , thanks much!!!
option is a reserved keyword in MySQL. Use backticks to escape it or choose a different name
... `option` = ...

MySQL LIKE query is not worked for search record with apostrophes

when i start searching a record from db i got an issue when apostrophes present in word
for that i used addslashes,mysql_real_escape_strin but not worked for that
<?php
include("lib/dbconn.php");
$list_query_main1="select * from table where name like '%".mysql_real_escape_string($_REQUEST['keyword'])."%'";
$list=mysql_query($list_query_main1);
echo mysql_num_rows($list);
?>
Zero results found but name present in DB give me solution.
you are getting mysql 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 'table' at line 1
because TABLE is reserved word. If you named your table like TABLE, you must use right mysql syntax
$list_query_main1="select * from `table` where `name` like '%".mysql_real_escape_string($_REQUEST['keyword'])."%'";
Adding if(!$list || mysql_errno() != 0) echo mysql_error(); after line $list=mysql_query($list_query_main1); will give you some info in case of a query failure
Otherwise myqsl_* is deprecated you should start using mysqli_* functions.
And change your last line
echo $mysql_num_rows($list);
and replace it with
echo mysql_num_rows($list);
If you want to call myqsl_num_rows() function

MySQL UPDATE syntax error with variables

Long story short, I'm trying to write a PHP code that will parse a text file into MySQL queries. Everything works fine except for the queries, which consist of UPDATE statements.
The entire code is kinda long, but if you want to see it - http://pastebin.com/xVR6ArD0
Here is just the part which is problematic :
while ($i<=$no_collumns)
{
$j = $i-1;
if (!
mysql_query
("UPDATE ResultsPredmet
SET ${parsed_collumns[$i]} = '${parsed_words[$j]}'
WHERE ${parsed_first_collumn} LIKE '${parsed_first_word}'")
)
{echo mysql_error()."\n"; break;}
// echo "\nUPDATE ResultsPredmet SET ${parsed_collumns[$i]} = '${parsed_words[$j]}' WHERE ${parsed_first_collumn} LIKE \"${parsed_first_word}\"";
$i++;
}
... where $parsed_collumns and $parsed_words are arrays of strings and $parsed_first_collumn and $parsed_first_word are strings.
I tried all combinations of quotes and escapes for the variables. I tried putting them in double quotes and escaping them, or double quotes and concatenating them, then maybe i thought it was the fact that I was comparing strings via the '=' operator so i tried with 'LIKE'. I googled for several hours and everywhere people said to use single quotation marks for variables so I tried that too and it didn't work.
In the end I echoed the queries and I get:
UPDATE ResultsPredmet SET grade = '10' WHERE name LIKE "Vildur"
UPDATE ResultsPredmet SET index = '117/2010' WHERE name LIKE "Vildur"
Updating table.
UPDATE ResultsPredmet SET grade = '6' WHERE name LIKE "NinoDoko"
UPDATE ResultsPredmet SET index = '132/2011' WHERE name LIKE "NinoDoko"
Updating table.
UPDATE ResultsPredmet SET grade = '10' WHERE name LIKE "Koco"
UPDATE ResultsPredmet SET index = '130/2011' WHERE name LIKE "Koco"
Done.
Which seem fairly fine to me. Other queries I got were the same only with the names with single quotes around them, or with no quotes or any other combinations.
The errors I get are :
Updating table.
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 'index = '117/2010' WHERE name LIKE 'Vildur'' at line 1
Updating table.
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 'index = '132/2011' WHERE name LIKE 'NinoDoko'' at line 1
Updating table.
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 'index = '130/2011' WHERE name LIKE 'Koco'' at line 1
Apparently, the server that I'm using is MariaDB 5.5, but after a bit of research I figured it would be similar to just generic MySQL, though I might be completely off. The "Updating table." is just a random echo in my code. I've also tried the query without indenting it, still got the same errors. The values I get for grade and index are strings - or at least I hope so, since I'm getting them with explode().
index is a reserved word
UPDATE ResultsPredmet SET `index` = '10' WHERE name LIKE 'Vildur'
http://dev.mysql.com/doc/refman/4.1/en/reserved-words.html

How do I correctly specify the WHERE clause in this SQL query written in PHP?

$query = "INSERT INTO directory_level_one (child_categories)
VALUES
('$category_name')
WHERE
category = '$parent'";
currently, I get the following error when I add the WHERE part in the above sql query.
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 category = 'Philosophy'' at line 4
You can't have a where clause for an Insert statement. Are you trying to update existing database records instead? In that case, use the Update statement.
INSERT statements don't have a WHERE clause.
Perhaps you want an UPDATE statement instead?
UPDATE directory_level_one
SET child_categories = 'your_category_name'
WHERE category = 'your_parent'
you can't use a where clause with an insert statement.
where clause can not be used in INSERT statment
please read this before preceding further http://dev.mysql.com/doc/refman/5.5/en/insert.html
What you want to do is this:
$query = "UPDATE directory_level_one SET child_categories='$category_name' WHERE category = '$parent'";
I think you might want to change your INSERT to an UPDATE

Categories