MySQL query error -unknown column - php

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.

Related

Stupid SQL error with no leads

Ok, I can't figure out why this doesn't work at all.
$get_data = mysql_query("SELECT * FROM chatbox ORDER BY ID WHERE message = 'bla'");
while($data = mysql_fetch_assoc($get_data)) {
*blablabla code*
}
This does NOT work. It gives me a
"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource" error. (with line number ofcourse)
When I remove the "WHERE message = 'bla'" part, it works fine. Tryed with and without ' things around bla and around messages. Message field does excist in my DB, so does the entry bla. Tryed it even with the ID field in my DB, with number 1, does not work at all. What is wrong with this simple line of code. Tryed to look almost everywhere, can't figure it out...
You are using WHERE clause after ORDER BY, it can not be work for you.
Modified Query:
SELECT * FROM chatbox WHERE message = 'bla' ORDER BY ID
You need to follow this sequence when you crease MYSQL SELECT Statement:
SELECT .. COLUMNS .. FROM .. WHERE .. ORDER
SELECT Manual Reference
Side Note:
Please use mysqli_* or PDO instead of mysql_* because this extension deprecated and not available in PHP 7.
Your query is wrong. You are using order by before where condition.
Try this query : "SELECT * FROM chatbox WHERE message = 'bla' ORDER BY ID"
And also migrate from mysql_* to mysqli_*.

How to use 'LIKE' operator in MySQL to retrieve result of a query using the value of $_GET

I would like to query my database so that it shows me the result of the query based on my PHP's superglobal $_GET. I have tried this:
LIKE '%".$_GET["name"]."%'"
AND
LIKE '%{$_GET["name"]}%'
However, it was in vain. Can anyone help me with this?
This is my php code:
$places = query( "SELECT * FROM places WHERE MATCH (postal_code, country_code, admin_name1, admin_code1, place_name) AGAINST (?) OR LIKE '%".$_GET["geo"]."%'", $_GET["geo"]);
The error message shows me:
Fatal 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 'LIKE '%akutan%''
It is not good idea to use global variable directly in your mysql query.
so, you must first assign it to some variable and use it
Like:
$getName = mysql_real_escape_string($_GET['name']);
$mysql = "SELECT * FROM places WHERE `postal_code` LIKE '%".$getName."%' ";
I hope it will help you
Use this
$mysql = "SELECT * FROM table WHERE input LIKE '%".mysql_real_escape_string($_GET['name'])."%' ";

Mysql issue,query not working

i'm having some trouble with mysql,so i posted with ajax the parameter "user" i got the sql connected for sure,but somewhy it doesnt do what i want it to do.Here's my code:
$ffs="select * from mex_szerzo where sznev=".$_POST["user"];
$vissza=mysql_query($ffs);
$sor=mysql_fetch_array($vissza);
$user=$sor["sznev"];
print ($user);
the $user is empy somewhy,the $_POST["user"] got value for sure,if i print it,it prints the actual user,i keep the users in a database,the username is stored as "sznev" for sure too,still the $user comes bk as an empy variable.
You're missing the quotes around your string value:
$ffs="select * from mex_szerzo where sznev=".$_POST["user"];
should be:
$ffs="select * from mex_szerzo where sznev='".$_POST["user"]."'";
You have no error checking in your code. That's why you didn't know what was wrong. Look into using mysql_error().
Or, better yet, stop using an obsolete API altogether. The mysql_* functions are deprecated. Look into using mysqli or PDO instead.
your query should be:
$ffs="select * from mex_szerzo where sznev='".$_POST["user"]."'";
You need to quote the string and make sure your quote types encapsulate the query properly (single vs double quotes).
$ffs="select * from mex_szerzo where sznev='".$_POST['user']."'";

SQL simple query displaying error (CONTAINS function)

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

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