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 ;)
Related
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?
In php, I'm trying to insert a value into one table, return an auto-incremented value, and then insert that value along with other values into a second table.
I'm running into a few problems. First, while there's a lot of ways of doing this in SQL, I have to do this with php's mysql functions. I'm afraid of weird errors if I combine multiple statements together. Second, like I mentioned, I need this to be done in one query, as it'll be used for a web application.
My current query is like this
INSERT INTO TABLE1 VALUES(*);
INSERT INTO TABLE2
SELECT max(AutoIncrementedColumn)
FROM TABLE1;
The problem I'm having is that mysql_query() doesn't support multi queries. Also, I believe mysql_escape_string() removes anything it believes to be a multi query, so even if I could somehow get mysql_query to believe my query is not a multi query, I'm still out of luck unless I write my own escape method.
Does anyone have any ideas on how to deal with this problem?
EDIT: Forgot to mention that I can't use mysql_insert_id because the column that's autoincrementing is of type Bigint.
I am trying to prevent duplicates from occuring using the following query, but it doesnt seem to work. Please could you tell me what the problem is?
INSERT IGNORE INTO Following SET `followingUserID` = '$accountIDToFollow', `followerUserID` = '$accountID'
INSERT IGNORE INTO
Following (`followingUserID`,`followerUserID`)
VALUE
('$accountIDToFollow','$accountID')
You were doing an UPDATE format before
If you are trying to do an update this is how it works
UPDATE followingUserID
SET
followingUserID = '$accountIDToFollow',
WHERE
followerUserID = '$accountID';
Of course you want to replace the were clause with the correct condition you want to do
As per MYSQL documentation,
If you use the IGNORE keyword, errors
that occur while executing the INSERT
statement are treated as warnings
instead. For example, without IGNORE,
a row that duplicates an existing
UNIQUE index or PRIMARY KEY value in
the table causes a duplicate-key error
and the statement is aborted. With
IGNORE, the row still is not inserted,
but no error is issued.
It means, the IGNORE does not prevent any record duplicate. You will have to put Unique constraints on your given fields.
I have an MS Access database (intolerably enough), and communicating with it through PHP (ODBC).
There is a DateTime field that I have to include in my INSERT statement. This field is NOT defined as "Required" in Access, meaning that it is indeed NULL-able, and in fact some of the rows in the Access database are already NULL.
The problem I'm having is simple: How to insert NULL through SQL? All the results I've found online have addressed it from something like Visual Basic or C#, whereas I'm using SQL through ODBC in PHP.
I have already tried the following:
INSERT INTO table_name (datetime_field) VALUES (NULL)
INSERT INTO table_name (datetime_field) VALUES (#NULL#)
INSERT INTO table_name (datetime_field) VALUES ('NULL')
INSERT INTO table_name (datetime_field) VALUES ('#NULL#')
INSERT INTO table_name (datetime_field) VALUES ('')
(There's about 30 other columns in my query.)
The exact error I get is 'Data type mismatch in criteria expression' when I try '' or NULL. The others return a parse error (understandably).
Please note that I have to include the field in the INSERT statement. The field has a default value, but in many cases the original data that I'm transporting has a NULL that must also be a NULL in the target database.
Thanks in advance!
Try the following. It works for me:
INSERT INTO sometable ( somedate, somethingelse )
SELECT Null AS Expr1, "foo" AS Expr2;
Basically, you are wrapping the null in the select query and letting SQL figure out how to represent it to the insert.
-- EDIT --
This SHOULD also work:
INSERT INTO sometable ( somedate, somethingelse )
values (Null , "foo");
But for some reason it doesn't with my default install.
On I hunch, I switched my DB from ANSI-89 to ANSI-92, and the VALUES method started working. I switched it back to ANSI-89, and it still works. Not only that, on ANY new database I create, it now also works. Weird... something in the installation must be getting changed, (and sticking) by the switching back and forth that's not just ANSI-89/92. This seems to be why we were getting different results.
You can switch the database ocwe by going to Office Logo->Access Options->OBJECT DESIGNERS->QUERY DESIGN. Change SQL Server Compatible Syntax (ANSI 92) - and checking "This database".
Ok, very odd.
I know you've already figured this out but there is also dbNull
INSERT INTO table_name (datetime_field) VALUES (DbNull.Value)
Try just leaving it blank
(values fld1,,fld2)
What are the libraries, you are using in order to talk to ODBC?
Could it be a problem with the syntax for null values, the way library interprets it?
See, if this page helps.
Note: I have not worked with PHP.
I have this type of error and
Try this.
INSERT INTO table_name (datetime_field) VALUES (DBNull.value)
It works fine for me.
I am using REPLACE INTO query to insert in to table but after executing query by using mysql_query() function and if I use last_insert_id() it is only giving me 0 value.
why this is happening so? and how can I overcome this behavior.
:Pankaj
You could try using INSERT INTO ... ON DUPLICATE KEY UPDATE instead. It accomplishes the same thing as REPLACE INTO, but in a single server-side operation. REPLACE INTO can end up causing two operations: delete the old one, insert the new one.
But regardless of the query type, you do have to ensure that the table's primary key is an auto_increment field. last_insert_id() does not work properly otherwise.
REPLACE INTO doesn't seem to affect the vaue that can be obtained via last_insert_id().
It seems to be the expected behavior, judging from this :
LAST_INSERT_ID() give wrong value after REPLACE INTO query
LAST_INSERT_ID()