SQL simple query displaying error (CONTAINS function) - php

I am trying to display all users with #devon.gov.uk address.
I am using following code:
SELECT * FROM `j25_users` WHERE `email` CONTAINS `#devon.gov.uk`
But 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 'CONTAINS `#devon.gov.uk` LIMIT 0, 30' at line 1
What could that indicate? SQL newbie here.

try this
SELECT * FROM `j25_users` WHERE CONTAINS (email,`#devon.gov.uk`)

You should use:
SELECT * FROM `j25_users` WHERE `email` like '%#devon.gov.uk%'

You tagged your post phpmyadmin, so while the others have correctly provided the SQL way of doing this, I'll just point out that phpMyAdmin also gives you a graphical interface. From the "Search" tab, just scroll down to the "email" row and change the dropdown to Like %...% and put #devon.gov.uk in the Value textbox. Then press Go.

You could try this instead:
SELECT * FROM `j25_users` WHERE `email` LIKE `%#devon.gov.uk%`;
The problem was the backtick `. the above code should have been:
SELECT * FROM `j25_users` WHERE `email` LIKE '%#devon.gov.uk%';

Related

Why it shows me error when preparing a sentence in PHP with MySQLi

I'm creating a sentence prepared in PHP, and I run into a rare syntax error, I do not know if it is breaching any of MySQL or why I show that error
The syntax is as follows, I want to sort by row and by ascending or descending type and limit the results
$query = "SELECT * FROM myTable ORDER BY ? ? LIMIT? ,?"
if($conn->prepare($query)){ .. } // error
The error is
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 ? , ?' at line 1
If you execute said statement in MySQL, it correctly throws the results
Parameters to ORDER BY are not values, and cannot be parametrised. One is a column reference, the other is a keyword.
For example do like this and try.
$query = "SELECT * FROM myTable ORDER BY column_name LIMIT 0,10";

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.

error in sql query for displaying records from table

I am trying the following query and it works fine
$sqlmsg=$Db1->query("SELECT COUNT(username) AS tmsgs FROM messages");
$tempmsg=$Db1->fetch_array($sqlmsg);
$thismemberinfo['msg']=$tempmsg[tmsgs];
display the output
$thismemberinfo[msg]
While when try to run the following query for the same table, it gives me errors:
$sqlmsg=$Db1->query("SELECT COUNT(read) AS tmsgs FROM messages");
$tempmsg=$Db1->fetch_array($sqlmsg);
$thismemberinfo['msg']=$tempmsg[tmsgs];
values for read are [0 or 1 ] always.
Follwing error is generated when i run the 2nd query:
Database error: Invalid SQL: SELECT COUNT(read) AS tmsgs FROM messages
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 'read) AS tmsgs FROM messages' at line 1)
Session halted.
TASK That i want to acheieve:
I have a table where user messages are stored,read= 1 , unread =0 ,
I want to display unread messages from each user , when he logs in.
thinking of something like this
$sqlmsg=$Db1->query("SELECT COUNT(read) AS tmsgs FROM messages WHERE username=$username");
read is a reserved word(s) in MySQL. You need to wrap them using backticks.
It should be..
$sqlmsg=$Db1->query("SELECT COUNT(`read`) AS tmsgs FROM messages");
read is a reserved keyword
use backtick for it as
`read`

MySQL query error -unknown column

My query string is
$chk_cookie="SELECT * FROM cookie_data_mst WHERE uniqid_client=5279f0addc835 AND cookie_data=3";
$chk_query=mysql_query($chk_cookie) or die(mysql_error());
this give the error unknown column.
if I put ' in value
'5279f0addc835'
It gives check manual for syntax error.
If I remove first condition i.e uniqid_client=5279f0addc835 then it runs normally.
If I do string like
$chk_cookie="SELECT * FROM cookie_data_mst WHERE uniqid_client=".5279f0addc835." AND cookie_data=3";
or
$chk_cookie="SELECT * FROM cookie_data_mst WHERE uniqid_client='".5279f0addc835."' AND cookie_data=3";
It gives the same check manual error....
Another thing if I run it on phpMyAdmin SQL it gives the desired result
what should I do ...I am not able to get error...
5279f0addc835 value I have created by php uniqid() function.
Try like this
$chk_cookie="SELECT * FROM cookie_data_mst WHERE uniqid_client='5279f0addc835' AND cookie_data=3";
I accepting all the answer but i thing should check your database data type and table data.if you have wrong datatype and blank field then it will give error.
You need to enclose uniqid_client=5279f0addc835 in single quotes as it is a VARCHAR type. Something like this
$chk_cookie="SELECT * FROM cookie_data_mst WHERE uniqid_client='5279f0addc835' AND cookie_data=3";
$chk_query=mysql_query($chk_cookie) or die(mysql_error());
Disclaimer: Stop using mysql_* functions as they are deprecated. Switch to MySQLi or PDO instead.

Have trouble with mySQL query

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';

Categories