I'm trying to perform a merge based on a parameter from a previous select within a php script but I"m getting the error "SQL0408 - Value for column,variable, or paramter QUANTITY not compatible"
In my destination table QUANTITY is data type INTEGER
In my select query, I'm casting the value as an int (which it already is in the table, I'm just casting everything to be safe)
cast(MAX(orqtyc) as int) AS QUANTITY,
Then in my MERGE I'm casting as INT
MERGE INTO HNORMANTEST.PLACEMENTS AS P
USING(VALUES(
CAST(:QUANTITY as INT),
))
Using this param
$params = [
":QUANTITY" => $row["QUANTITY"],
];
Why would it say it's not compatible?
Did you try it by putting directly value instead of via param and see if it works or not.
Another thing you can try is remove the first casting which may not be needed. As you said QUANTITY is already a INT datatype.
Please try both the variation. If both variation givens same error then there might be some product limitation/bug.
You need to pass on the DB2 version where you have tried to look further in it.
Related
So recently I'm making a simple CRUD app with Codeigniter.
My table has id field that is an auto increment field.
My model has a function called getById.
public function getById($id){
return $this->db->get_where($this->_table,["id" => $id])->row();
}
In my table, I have 1 field with ID of 1.
If I try to query with wrong ID, it return empty.
However, when I try to input something like 1 followed with string like
1test
it did return the field with id 1 in my database. I don't know if this is how it should be or is it a bug.
Surely Codeigniter is casting your string to integer.
PHP intval() manual says:
Strings will most likely return 0 although this depends on the leftmost characters of the string. The common rules of integer casting apply.
So, if you need to throw an error on invalid entries, you must validate them first, maybe using is_int() to allow only integer type or is_numeric() to allow strings containing only numbers.
I'm having a table whith 'id' as pk of type int.
When I'm doing the folowing mysql query in php activerecord
Location_cat::find_by_sql("select concat('#',id) as 'id', text FROM location_cat");
it returns 'id' as 0, instead of '#142'for example.
Does anyone knows this behavior?
You are trying to overwrite the 'id' field, but this will give you issues. An activerecord object knows it's own primary key (i'd assume the id-field), and uses it for all sorts of stuff. If you try to select something else into that field, this will not work.
For instance, you get the object with id 1. This is renamed to #1 by you. now AR thinks it is an object with a primary key that is #1. Which is not true
Bottomline is, you should not put something else in the id field if you want to work with an actual activerecord object.
What you might want to do is just get the data, but on returning the $object->id field, you can add the #. For instance, make a magic getter for id (I believe AR allows you to name this getId(), but otherwise, fix it in __get().
Lets Cast the id into a string before concatenating
SELECT CONCAT('#', CAST(id AS CHAR(15))) AS nid
FROM location_cat
I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html
I'm new in cassandra and I wanna get the values from a column family, where the key is a TimeUUIDType.
I'm using PHP with PHPCassa, and I can insert(set) correctly in the column family, generating uuid with the function:
$key = CassandraUtil::uuid1();
The problem happen when I try to do a get in the column family, because i have and uuid in String format( something like that):
$uuidString= "e2658820-69f2-11e1-af9a-95dd4f324d9";
I would like to know if is possible cast or transform an String form to a valid uuid for cassandra in php or phpcassa, because my purpose is in another page create the correct uuid from the $uuuidString.
Thanks.
CassandraUtil::import($uuidString) will handle that.
Edit
As of phpcassa 1.0+, \phpcassa\UUID::import() is the proper method.
I have a MYSQL table with an ENUM field named "offset" and some other columns. The field is defined as:
ENUM(0,1), can be NULL, predefined value NULL
Now I have two server. A production server and a development server and the same PHP script used to create and to update the database.
First step: the application create the record witout passing the "offset" in the CREATE query.
Second step: the application ask to the user some data (not the "offset" value), read the row inserted in step one and make an array, update some field (not the "offset" field), create a query in an automated fashion and save the row again with the updated values.
The automated query builder simple read all the field passed in an array and create the UPDATE string.
In both systems I obtain this array:
$values = array(... 'offset' => null);
and convert it in this same query passing the values in the mysql_real_escape_string:
UPDATE MyTable SET values..., `offset` = '' WHERE id = '10';
Now there is the problem. When i launch the query in the production system, the row is saved, in the development system I got an error and the db says that the offset data is wrong without saving the row.
From phpmyadmin when I create the row with the first step, it shows NULL in the offset field. After saving the field in the system which give no errors, it show me an empty string.
Both system are using MySQL 5 but the production uses 5.0.51 on Linux and development use 5.0.37 on Windows.
The questions:
Why one system give me an error an the other one save the field ? Is a configuration difference ?
Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?
Why one system give me an error an the other one save the field ? Is a configuration difference ?
Probably. See below.
Why when I save the field which is an enum "0" or "1" it saves "" and not NULL ?
According to the MySQL ENUM documentation:
The value may also be the empty string ('') or NULL under certain circumstances:
If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a "normal" empty string by the fact that this string has the numeric value 0. ...
If strict SQL mode is enabled, attempts to insert invalid ENUM values result in an error.
(Emphasis added.)
strager's answer seems like a good explanation on why your code behaves differently on the 2 environments.
The problem lies elsewhere though. If you want to set a value to NULL in the query you shound use exactly NULL, but you are using mysql_real_escape_string() which result is always a string:
$ php -r 'var_dump(mysql_real_escape_string(null));'
string(0) ""
You should handle this differently. E.g:
$value = null
$escaped_value = is_null($value) ? "NULL" : mysql_real_escape_string($value);
var_dump($escaped_value);
// NULL
Some DB layers, like PDO, handle this just fine for you.
If you want it to be NULL, why don't you do this in the first place:
UPDATE MyTable SET values..., `offset` = NULL WHERE id = 10;