How to insert this 917678904#tmomail.com into mysql database? .
I tried putting single quotes around it and without the single quotes but both methods fail.
// This is my query:
$n = 917678904#tmomail.com;
$table = "invites";
if(!mysql_query("INSERT into $dbname.$table (accepted_by, accepted_on, phone, email, reffer)
VALUES('NULL', 'NULL', 'NULL', `$n`, 'NULL'")){
echo mysql_error();
}
This is the table structure
accepted_by VARCHAR (50) NOT NULL
accepted_on VARCHAR (45) NOT NULL
phone VARCHAR (11) NULL
email VARCHAR (250) NULL
refferer CHAR (3) NOT NULL)
This is the error message with SINGLE quotes around $n:
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 2
I am using MYSQL version 5.0
`$n`
in your values field is incorrect. Backticks are used to escape field names, which the value of $n almost certainly isn't. It should be using regular quotes:
INSERT ... VALUES (..., '$n', ...)
^--^--
As well, 'NULL' is also incorrect. That will try to insert the literal characters N, U, L, L into your db table. For an actual SQL null value, remove the quotes:
... VALUES(NULL, NULL,....)
^^^^
And lastly, you say your table structure has refferer, but your query string is using reffer. Maybe just a typo, but worth pointing out.
Related
When I tried to insert into a table in Mysql with the following fields direction, from, to, message, I got the following error
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 'from, to, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','asas')'
Query:
INSERT INTO corporate.sms (direction, from, to, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','test message');
Is there any other way to do the insertion without changing the column names?
Since field name such as 'from' and 'to' is reserved by MySQL's SQL syntax,
you need to wrap those field names by ` ( backquote ).
So your SQL will be
INSERT INTO corporate.sms (direction, `from`, `to`, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','test message');
from and to are reserved keywords in SQL.
The solution is quite simple. Just encapsulate the from and to columns in your query between two Grave Accents(``), which is the key above Tab in your keyboard. That way, SQL Parser distinguishes the column names with reserved keywords and performs the insertion.
The query becomes:
INSERT INTO corporate.sms (direction, `from`, `to`, message) VALUES ( 'outgoing','FROM_NUMBER','TO_NUMBER','test message');
This is producing an sql syntax error, don't know why:
INSERT INTO dnc_temp (number, release) VALUES ('07938347', '2014-07-10 23:50:12')
The fields I'm inserting into are INT(15) and DATETIME
Thanks
RELEASE is MySQL Reserved keyword
To use a reserved keyword as a column name use ` around the keyword
Try this :-
INSERT INTO dnc_temp (`number`, `release`) VALUES ('07938347', '2014-07-10 23:50:12')
Check this LIVE SQLFIDDLE
column name release is Reserve character that's why producing syntax error. check Live SQLFiddle Link.
MySQL
CREATE TABLE dnc_temp(
num INT(8),
rel DATETIME
);
INSERT INTO dnc_temp (num, rel) VALUES ('07938347', '2014-07-10 23:50:12');
SELECT * FROM dnc_temp;
Result
NUM REL
----------- ----------------------------
7938347 July, 10 2014 23:50:12+0000
use the ` character to escape the reserved word "release".
INSERT INTO dnc_temp (number, `release`) VALUES ('07938347', '2014-07-10 23:50:12')
I have only spaces as values of rows in a column of length 121 chars.
I want to write a query in oracle to check whether the column string is only spaces.
for e.g. if say for column address. a row contains only spaces(all 121 chars are spaces).
i want a query that will check if the row contains only spaces.
select * from table where address <> ' ';
but this isnt working it only checks for 1 space. i want the query to check for all 121 spaces.
You could just run a TRIM on the column. Depending on your DBMS, the Null string is treated as either a NULL or as an empty string ''. But for Oracle where the empty string is treated as a NULL, you should be able to do:
select * from table where LTRIM(RTRIM(address)) IS NOT NULL
Note that TRIM removes all whitespace, not just the space character.
If you're checking for 121 spaces as a learning exercise then that's OK, but if you're storing 121 spaces in a column for a production application you may have a bad design.
If the column type is CHAR it should probably be changed to VARCHAR (or VARCHAR2 in Oracle). If it already is VARCHAR/VARCHAR2 you should store an empty string as, well, an empty string.
If you need the value padded to 121 spaces, take care of that when you query it:
Oracle: SELECT RPAD(NVL(address, ' '), 121) FROM myTable
MySQL: SELECT RPAD(address, 121, ' ') FROM myTable
If you want to find out if the value is empty, remember that Oracle treats an empty string as NULL. MySQL treats it as an empty string:
Oracle: SELECT * FROM myTable WHERE address IS NULL
MySQL: SELECT * FROM myTable WHERE address = ''
use REPLACE() function and compare it's result to null
SELECT * FROM table WHERE REPLACE(address,' ') IS NULL;
Fiddle
I'm trying to teach myself MySQL while working on a project at the same time. I'm using phpMyAdmin.
I'm getting the 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 ''ps_category' ('id_category', 'id_parent', 'id_shop_default', 'level_depth', 'nl' at line 1"
My code:
INSERT INTO 'ps_category'
('id_category', 'id_parent', 'id_shop_default',
'level_depth', 'nleft', 'nright', 'active',
'date_add', 'date_upd', 'position', 'is_root_category')
VALUES (6,2,1,0,0,0,1,'2012-04-12 15:12:54','2012-04-12 15:12:54',1,0)
UPDATE:
I took off the single quotes and still getting the same error:
INSERT INTO ps_category
('id_category', 'id_parent', 'id_shop_default',
'level_depth', 'nleft', 'nright', 'active',
'date_add', 'date_upd', 'position', 'is_root_category')
VALUES (6,2,1,0,0,0,1,'2012-04-12 15:12:54','2012-04-12 15:12:54',1,0)
INSERT INTO `ps_category` (`id_category`, `id_parent`, `id_shop_default`, `level_depth`, `nleft`, `nright`, `active`, `date_add`, `date_upd`, `position`, `is_root_category`) VALUES (6,2,1,0,0,0,1,'2012-04-12 15:12:54','2012-04-12 15:12:54',1,0)
You are using a single quote on the table name. It should be ticks or nothing. It should be noted, the ticks help to ensure properly reading the table name. If you name your table a mysql reserved word, the ticks will prevent it from erroring
The table name should not be entered as a string literal, either remove these '' or put two '' and '' around it like so
INSERT INTO ps_category ...
Or
INSERT INTO `ps_category` ...
Table names should not be quoted
I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?
You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');
Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.
You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.
Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)