Zend: How to insert NULL values into MySQL - php

I am using Zend Framework with MySQL,Apache and Ubuntu 9.04.
I am trying to insert NULL values into database like this:
$personObj->setPersonId( '1' );
$personObj->setPersonEmail('NULL');
$personObj->save();
But 'NULL' is stored in database as string and not NULL.
When I use this:
$personObj->setPersonId( '1' );
$personObj->setPersonEmail(NULL);
$personObj->save();
But nothing happens and previous entry is unchanged.
What should I do to insert NULL values into MySQL?

If you are not modifying any of the values after they are assigned then
new $personObj->setPersonEmail(new Zend_Db_Expr('NULL'));

First thought would be straight passing in the null keyword, without quotes around it. As pavium said, the quotes around it turn it into a string.

I think putting NULL in quotes is what makes it look like a string.
I don't know about your method, but for a direct insert though a mysql INSERT command, the single quotes around a NULL are incorrect.

Related

Removing/disabling backticks on variable in Active Record query in CodeIgniter

I'm trying to make a "between" query on my database using the Query Class of CodeIgniter, however, when adding a variable to the where clause, it adds backticks to the variable.
$this->db->select(TABLE_DISCOUNTSCARRIER.'.discount')->select(TABLE_DISCOUNTSCARRIER.'.idCarrier')
$this->db->from(TABLE_DISCOUNTSCARRIER);
$this->db->join(TABLE_DISCOUNTS, TABLE_DISCOUNTSCARRIER.'.idDiscount='.TABLE_DISCOUNTS.'.idDiscount');
$this->db->where(TABLE_DISCOUNTSCARRIER.'.idCarrier', $carrier);
$this->db->where($data['from'].' BETWEEN '.TABLE_DISCOUNTS.'.from AND '.TABLE_DISCOUNTS.'.to');
$this->db->or_where($data['to'].' BETWEEN '.TABLE_DISCOUNTS.'.from AND '.TABLE_DISCOUNTS.'.to');
Which is being parsed into this (the last two lines)
SELECT
discountbycarrier.discount,
discountbycarrier.idCarrier
FROM (discountbycarrier)
JOIN discounts
ON discountbycarrier.idDiscount=discounts.idDiscount
WHERE `discountbycarrier`.`idCarrier` = '6'
AND `5` BETWEEN discounts.from AND discounts.to
OR `10` BETWEEN discounts.from AND discounts.to
Already tried setting the $this->db->_protect_identifiers=false; but it removes the backticks on the rest of the statements but not the variables. Already tried using the intval() of the variable but neither this works.
As you can see the variable $carrier is correctly being parsed as integer.
Any ideas? Thanks in advance.
Change this
discounts.from
to
discounts.`from`
Or better (best practice) don't use reserved keywords for table columns, the word FROM is part of the query language, in other words.
Backticks are used to escape reserved keywords and spaces.
UPDATE
Something like this ( although I didn't look the documentation )
$this->db->where('? BETWEEN '.TABLE_DISCOUNTS.'.from AND '.TABLE_DISCOUNTS.'.to', $data['from']);
add this before the query
$this->db->_protect_identifiers=false;

postgresql insert null value on query

In my php code query, i got some error when i will place a null value
into the table. The column names "number1","number2", and "number3" are integers and can have a null value.
if there is no null value, the query works
query is like this
insert into table(number1,number2,number3) values (1,2,3);
but when I leave a blank value on the input form on the PHP,
for example I will not place value for column "number2", the query will look
like this.
insert into table(number1,number2,number3) values (1,,3);
it says ERROR: syntax error at or near ","
SQL state: 42601
Does anybody have an idea how to solve this?
this query is likely to be the solution but is there any other way?
insert into table(number1,number2,number3) values (1,null,3);
because i got so many variables like that and am a bit lazy to place some conditions like when that variable returns a blank value then value="null"
You insert NULL value by typing NULL:
INSERT INTO table(number1,number2,number3) VALUES (1,NULL,3);
If you have a variable and when that variable is empty you want to insert a NULL value you can use NULLIF with the variable enclosed in single quotes to prepare for that (this is somewhat dirty solution as you have to treat the variable as an empty string and then convert it to integer):
INSERT INTO table(number1,number2,number3) VALUES (1,NULLIF('$var','')::integer,3);

Codeigniter activerecord selecting null into a dummy column

I´d like to add an extra column to the select for formatting purposes. The problem is that when I do
$this->db->select("NULL as ExtraColumn1")
codeigniter treats NULL as a column, so when it generates the query it's something like
SELECT `NULL` AS ExtraColumn1 ...
which of course returns a DB error. The same happens when I try
$this->db->select(" '' as ExtraColumn1")
Is there any way of doing it using activerecord?
Thanks
Tell CodeIgniter not to wrap fields in ticks. You do this by passing false as the second parameter in select():
$this->db->select("NULL as ExtraColumn1", false);
From the manual:
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

data no inserting into mysql database due to difference in 'quotes'

I am having a very strange problem inserting values into my mysql database, using php, so i was running a test, the simplest of the simple insert; the following doesnt work:
<?php
include("config.php"); // put the *FULL* path to the file.
mysql_query("INSERT INTO 'lms'.'test2' ('trn') VALUES ('17')");
?>
However the following works:(Note the difference in single quotes)
<?php
include("config.php"); // put the *FULL* path to the file.
mysql_query("INSERT INTO `lms`.`test2` (`trn`) VALUES ('17')");
?>
I really can't see what the problem is could I get sum assistance please
You don't need to encapsulate tables within a query unless they have space or they are reserved words.
INSERT INTO 'lms'.'test2' ('trn') VALUES ('17')
// This makes no real sense to the db. It should be:
INSERT INTO lms.test2 (trn) VALUES ('17')
If the column trn accepts numbers, it really should be:
INSERT INTO lms.test2 (trn) VALUES (17)
With MySQL, you can use the tilted quote character to encapsulate names, but not strings. To enter a string in the query you will have to use normal quotes like '.
You can to this:
select `someTable`.`someColumn` from `someTable`
but not this:
select someTable.someColumn from someTable where myName=`Tommy`;
The correct use would be:
select someTable.someColumn from someTable where myName='Tommy';

bar/pipe delimited field

Anyone know how to insert bar/pipe delimited array in an array field?
With comma its working but when I change it to bar it produces error "malformed array literal"
example (works):
insert into table (arrayfield) values ('{"var1","var2","var3"}')
example (doesn't work):
insert into table (arrayfield) values ('{"var1"|"var2"|"var3"}')
btw i'm using postgres 8.2 and PHP and i can't use comma as delimiter for various reasons.
If you can use values without quotes because you know that no pipe is in your data, then you can use:
insert into table (arrayfield) select string_to_array('var1|var2|var3','|');
If you need that your values are quoted on your pipe-delimited string then it gets complicated. If that's the case then I think you should format and quote properly your array using ARRAY['val1','val2','val3'] syntax in PHP.

Categories