I'm trying to do a simple select on a table with a simple where clause. Basically:
$query = "Select * from devices where device_id = 'abcdefghijklmnopqrstuvwxyz000000'";
When I try to execute the query, I get the 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 '' at line 128
If I shorten $id by 1 character, it works, or if I try to query a different field, it's fine. Obviously this is something to do with the datatype being stored in the table. The field device_id is char(32), so I understand if the query wouldn't take values greater in length than that, but $id has a length of 32.
Even copying the value from the device_id column in phpMyAdmin and pasting it as the value for $id in my php doesn't work. Something seems fishy... What's going on, and how can I fix it?
I've gotten some comments about changing the datatype, and stating that 'abcdefghijklmnopqrstuvwxyz000000' is too large to store in the table. However, it isn't.
Let me clarify my question:
The value stored in this column in the db is 'abcdefghijklmnopqrstuvwxyz000000'.
The value in my query is the exact same value: 'abcdefghijklmnopqrstuvwxyz000000';
Both have a strlen of 32, and the datatype of that column is char(32).
Why will the table store the value 'abcdefghijklmnopqrstuvwxyz000000', but not let me query against that value? This doesn't seem correct.
The length of Id Column is small to have $id = 'abcdefghijklmnopqrstuvwxyz000005' in it, you just have to change the column ID data type, maybe varchar(64) will be enough.
You may mistyped the value to your database?
Can you check this
with same values in your database?
Related
I wanted to update a column in my database table, the update should just add a numeric value to the existing one.
But this time around, I'm writing the query with CodeIgniter Query builder, the issue is that when I run the script, CodeIgniter throws an Sql Exception below:
"message": "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 '11:01:37\nWHERE `user_id` = '26'' at line 1"
As you can see, it added a new line character to the query string.
The PHP code below is the query in CodeIgniter
$userModel->set('reputations', 'reputations+10', false)
->where('user_id', $user_id)
->update();
One thing I noticed is that if I removed the false (the third parameter) which tells CodeIgniter not to escape the column name, there won't be any error, instead '0' will be updated at reputation column.
I don't know what the problem might be, I could have moved on by writing a custom query, but, I wanted to be sure that I'm not doing something wrong.
P.S: custom one will look like this:
UPDATE users
SET reputations = reputations + 10 WHERE user_id = $user_id
Note: in the above error message you might be wondering where the digits in the error came from i.e
'11:01:37 in '11:01:37\nWHERE user_id
It is the value of a column in my table which is also updating along side reputation column.
Thanks amigos.
Could it be your code editor generating the newline?
Anyways, one fast way to avoid the problem is to use codeigniter query method:
$userModel->query("UPDATE `users` SET `reputations` = reputations + 10 WHERE `user_id` = $user_id)
Not the cleanest solution but it makes sure it works! :)
Mattia
I have a little/big problem with dynamic SQL query/ row datas to column name.
I have this link:
http://sqlfiddle.com/#!9/b4478/1
But if i adding VERY more datas, the query not working.
I getting 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 'FROM test
GROUP BY nid' at line 2
my database is big currently.
in the next days i making the empty database.
i adding to names(another table, connected id to "nid"), date(datum), amount(osszeg).
week to week the data is too much(maybe but once too much, and then not working the query)
and i need a simple/single PHP query code for this query.
Thanks for helping and sorry my bad english.
Very thanks!
Without seeing the actual SQL statement, we're just guessing. But I suspect that you are running into documented behavior of the GROUP_CONCAT function, which returns a string of maximum length specified by the MySQL variable GROUP_CONCAT_MAX_LEN. The default is 1024 bytes.
Reference: https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_group_concat_max_len
SET group_concat_max_len = 1024;
For debugging, you could have the MySQL stored program to emit the contents of #sql, instead of attempting to prepared it, e.g.
SELECT #sql;
You may want to consider comparing the length (in bytes) of the string returned from the GROUP_CONCAT function is not equal to the maximum length GROUP_CONCAT_MAX_LEN, to check whether the value has been truncated.
On a different note, I would opt for returning this result as a set of rows, rather than as columns on a single row. And I would do any required translation for display on the client side, rather than doing it in the database.
I have a small website and I want to store the email of my contacts in a MySQL database.
I am using PHP and MySQL. Each time I try, it gives me following error. However when I remove the # and . (dot) it works fine. I really need a help on this one, please tell me what mistake I am doing and it would be very helpful if the code is given.
I already used VARCHAR only. It keeps showing up the error below:
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 '#gmail.com( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id),
name VARCHAR(3' at line 1
you can use vachar datatype with 1-255 length.
And for more information if your value's length is more then 255 then use text datatype
Use varchar and wrap the email in quotes so it can be treated as a string like so:
'Email#gmail.com'
That's it bro.
Besides the error with the #, you still have a query that won't work:
gmail.com( id INT....
shouldn't that be
gmail.com', ( id INT....?
The fact that your database can't handle a # can be caused by the characterset of the database. Is it UTF-8 Unicode (utf8)?
And what is the type of the record where you want to store the mail?
a varchar(50) would be enough for storing email..
Then in your query all the values to insert must wrapped by '' or ""
e.g
$sql='INSERT INTO TABLE1 VALUES("Value1","EMAIL#GMAIL.com","VALUES3")'
I am trying to detect inserting result.
When I tried to insert with INSERT INTO table (1,2,'t') this will result in inserting error because of the third column of my table is decimal type,but mssql_query will return true.
but when I tested with INSET INTO table (1,2,'t') this will return false,like it should.
Why the first statement returning true? and how can we check that it is an error not true!!
Correct syntax is:
insert into table_name (column_name1, column_name2, ..)
VALUES (value_of_column1, value_of_column2, ..)
As you already know ordering part is not required, but i highly suggest you to do ordering first and then give values to it.
mysql_query return false on error and you can get error by using mysql_error function.
And keep this in your mind that you should surround values by quotations only when you are filling columns with type of varchar/char/date/datetime..
Other types like boolean, int, decimal and.. should be provided without quotations.
Hope it solve your problem ;)
I have a website with a sales and wanted page, which uses a query to return all of the sales & wanted ads into a recordset. It's been working for 4-5years without incident, but suddenly stopped working on Friday. My ISP tell me they have implemented v5 of MySQL, which seems to have caused the problem.
The query is below:
$query = "select * from $table order by uidno desc limit $from,$max_results";
It's executed via the following command
$recordset = mysql_query($query);
if($recordset == false)
{
echo("Could not retrieve comment. Please try later<br>");
echo("060211<br>");
return;
It's no longer able to load the comments into the recordset. Also the statement to populate the table is no longer populating the fields in the table correctly, though a new row is being created.
The statement is below:
$inputdata = "INSERT INTO $table(date,name,email,suggestion) values('$today','$inputname','$email','$suggestion')";
And it is executed via:
$outcome = mysql_query($inputdata);
The structure of the table is as follows:
uidno int(11) extra=AUTO_INCREMENT Null=no default = none
date date default 0000-00-00
Name varchar(60)
Email varchar (60) Null=yes Default = NULL
Suggestion blob attrbutes=binary null=no
Please help - I don't understand what changes I need to make to the syntax to make these queries compatible with MYSQL v5.
Update:
I added the echo mysql_error(); and it appears to output the following:
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 '-10,10' at line 1
So this indicates an error in the syntax - but I've no idea what the error is.
http://dev.mysql.com/doc/refman/5.0/en/select.html
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
Column, index, stored routine, and
event names are not case sensitive on
any platform, nor are column aliases
so your lowercase column names in code and upper case column names in mysql structure should not be the problem.