NULLS vs Empty result in PHP
NULLS vs Empty result in PHP
am 23.09.2009 13:58:26 von Dan Shirah
--000e0cd23cb2c3cfe104743d6e88
Content-Type: text/plain; charset=ISO-8859-1
Morning!
Just a quick question.
Say I have a column in my database that could contain NULLS, empty spaces,
or an actual value.
If I do:
$my_query = "SELECT my_column FROM my_database WHERE 1 = 1";
$my_result = ifx_query($my_query, $connect_id);
while($row = ifx_fetch_row($my_result)) {
$my_column = trim($row['my_column']);
if ($my_column != "") {
echo "Test";
}
}
The way PHP assigns the query results to the $my_column variable, wouldn't;
if ($my_column != "") not do anything for the rows that contained NULLS or
empty spaces?
--000e0cd23cb2c3cfe104743d6e88--
RE: NULLS vs Empty result in PHP
am 23.09.2009 14:26:52 von Samrat Kar
Use var_dump before processing your result.
Regards,
Samrat Kar
FRD, BARC
Tel: 022-25597295
Alternate Email: esamrat@yahoo.com
-----Original Message-----
From: Dan Shirah [mailto:mrsquash2@gmail.com]
Sent: Wednesday, September 23, 2009 5:28 PM
To: PHP General
Subject: [PHP] NULLS vs Empty result in PHP
Morning!
Just a quick question.
Say I have a column in my database that could contain NULLS, empty spaces,
or an actual value.
If I do:
$my_query = "SELECT my_column FROM my_database WHERE 1 = 1";
$my_result = ifx_query($my_query, $connect_id);
while($row = ifx_fetch_row($my_result)) {
$my_column = trim($row['my_column']);
if ($my_column != "") {
echo "Test";
}
}
The way PHP assigns the query results to the $my_column variable, wouldn't;
if ($my_column != "") not do anything for the rows that contained NULLS or
empty spaces?
No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.409 / Virus Database: 270.13.112/2388 - Release Date: 09/22/09
17:54:00
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: NULLS vs Empty result in PHP
am 23.09.2009 14:28:11 von Ralph Deffke
using empty() is ´the right way to check a var for NULL or ""
however, it also depends what MySQL has got as setuo definition for empty
fields. on textfields u can define an epmty string as default.
ralph_deffke@yahoo.de
"Dan Shirah" wrote in message
news:a16da1ff0909230458o30d66186m75fc4fd0d1972601@mail.gmail .com...
> Morning!
>
> Just a quick question.
>
> Say I have a column in my database that could contain NULLS, empty spaces,
> or an actual value.
>
> If I do:
>
> $my_query = "SELECT my_column FROM my_database WHERE 1 = 1";
> $my_result = ifx_query($my_query, $connect_id);
>
> while($row = ifx_fetch_row($my_result)) {
> $my_column = trim($row['my_column']);
>
> if ($my_column != "") {
> echo "Test";
> }
> }
>
> The way PHP assigns the query results to the $my_column variable,
wouldn't;
> if ($my_column != "") not do anything for the rows that contained NULLS
or
> empty spaces?
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: NULLS vs Empty result in PHP
am 23.09.2009 15:01:08 von Dan Shirah
--000e0cd25d82f3ee2004743e4e0e
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
>
> using empty() is =B4the right way to check a var for NULL or ""
>
> however, it also depends what MySQL has got as setuo definition for empty
> fields. on textfields u can define an epmty string as default.
>
> So say these are the first three results of my query:
"some text" //The column actually has data
" " //The column contains a series of spaces
"NULL" //The column contains a NULL value
As long as I trim() the result, if (!empty()) would skip any results where
the column contains " " OR "NULL", correct?
--000e0cd25d82f3ee2004743e4e0e--
Re: Re: NULLS vs Empty result in PHP
am 23.09.2009 15:28:06 von Robert Cummings
Ralph Deffke wrote:
> using empty() is ´the right way to check a var for NULL or ""
Empty will also match a string containing 0. And, to me, that doesn't at
all seem empty :)
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Re: NULLS vs Empty result in PHP
am 23.09.2009 15:34:37 von Mert Oztekin
--_000_E2C046087E10D943811A0BD0A4E8316D1B57CD11FAankaraanado lu_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
aWYgaXQgY2FuIHRha2Ugb25seSBudW1lcmljIHZhbHVlcyB1IGNhbiB1c2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gdGhpcyBtZXNzYWdlIGluIGVycm9yLCBwbGVhc2UgaW1tZWRpYXRl bHkgbm90aWZ5IHRo
ZSBzZW5kZXIgYW5kIGRlbGV0ZSBpdCBmcm9tIHlvdXIgc3lzdGVtLiBPdXIg Y29tcGFueSBoYXMg
bm8gbGVnYWwgcmVzcG9uc2liaWxpdHkgZm9yIHRoZSBjb250ZW50cyBvZiB0 aGUgbWVzc2FnZSBh
bmQgaXRzIGF0dGFjaG1lbnRzLiBPdXIgY29tcGFueSBzaGFsbCBoYXZlIG5v IGxpYWJpbGl0eSBm
b3IgYW55IGNoYW5nZXMgb3IgbGF0ZSByZWNlaXZpbmcsIGxvc3Mgb2YgaW50 ZWdyaXR5IGFuZCBj
b25maWRlbnRpYWxpdHksIHZpcnVzZXMgYW5kIGFueSBkYW1hZ2VzIGNhdXNl ZCBpbiBhbnl3YXkg
dG8geW91ciBjb21wdXRlciBzeXN0ZW0uDQo=
--_000_E2C046087E10D943811A0BD0A4E8316D1B57CD11FAankaraanado lu_--
RE: Re: NULLS vs Empty result in PHP
am 23.09.2009 15:37:45 von Ashley Sheridan
On Wed, 2009-09-23 at 16:34 +0300, Mert Oztekin wrote:
> if it can take only numeric values u can use is_numeric()
>=20
> also i suggest that you not to use nulls in dbs. instead, use "not null" =
and "default value" property. Its more simple and more effective for both m=
anaging your database and coding your program.
>=20
>=20
> -----Original Message-----
> From: Robert Cummings [mailto:robert@interjinn.com]
> Sent: Wednesday, September 23, 2009 4:28 PM
> To: Ralph Deffke
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Re: NULLS vs Empty result in PHP
>=20
> Ralph Deffke wrote:
> > using empty() is ´the right way to check a var for NULL or ""
>=20
> Empty will also match a string containing 0. And, to me, that doesn't at
> all seem empty :)
>=20
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>=20
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>=20
>=20
>=20
> ________________________________
> Bu mesaj ve ekleri, mesajda gönderildiÄi belirtilen kiÅi/k=
iÅilere özeldir ve gizlidir. Size yanlıÅlıkla ula=
ÅmıÅsa lütfen gönderen kisiyi bilgilendiriniz ve m=
esajı sisteminizden siliniz. Mesaj ve eklerinin içeriÄi ile =
ilgili olarak Åirketimizin herhangi bir hukuki sorumluluÄu bulunm=
amaktadır. Åirketimiz mesajın ve bilgilerinin size deÄi=
ÅikliÄe uÄrayarak veya geç ulaÅmasından, b=C3=
=BCtünlüÄünün ve gizliliÄinin korunamamasÄ=
=B1ndan, virüs içermesinden ve bilgisayar sisteminize verebilece=
Äi herhangi bir zarardan sorumlu tutulamaz.
>=20
> This message and attachments are confidential and intended for the indivi=
dual(s) stated in this message. If you received this message in error, plea=
se immediately notify the sender and delete it from your system. Our compan=
y has no legal responsibility for the contents of the message and its attac=
hments. Our company shall have no liability for any changes or late receivi=
ng, loss of integrity and confidentiality, viruses and any damages caused i=
n anyway to your computer system.
Or you could leave the database with the nulls in, and cast the value
explicitly as an integer with intval() or floatval() which will always
give you a numeric value.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: NULLS vs Empty result in PHP
am 23.09.2009 16:22:04 von Dan Shirah
--000e0cd23cb269cdc004743f7048
Content-Type: text/plain; charset=ISO-8859-1
>
> if it can take only numeric values u can use is_numeric()
>
> also i suggest that you not to use nulls in dbs. instead, use "not null"
> and "default value" property. Its more simple and more effective for both
> managing your database and coding your program.
>
>
It's a CHAR (60) field.
This database has been around for close to a decade and I'm not the DBA for
it. And with several billion records and 1000's of tables I don't see them
getting updated/changed. (The gov't doesn't like change)
--000e0cd23cb269cdc004743f7048--
Re: NULLS vs Empty result in PHP
am 23.09.2009 16:42:09 von List Manager
Dan Shirah wrote:
> Morning!
>
> Just a quick question.
>
> Say I have a column in my database that could contain NULLS, empty spaces,
> or an actual value.
>
> If I do:
>
> $my_query = "SELECT my_column FROM my_database WHERE 1 = 1";
> $my_result = ifx_query($my_query, $connect_id);
>
> while($row = ifx_fetch_row($my_result)) {
> $my_column = trim($row['my_column']);
>
> if ($my_column != "") {
> echo "Test";
> }
> }
>
> The way PHP assigns the query results to the $my_column variable, wouldn't;
> if ($my_column != "") not do anything for the rows that contained NULLS or
> empty spaces?
>
From reading the other responses to this thread, it seems that you want to
"skip" or "exclude" rows in the results where my_column === null.
If this is correct, why not do it in the SELECT statement to begin with?
$my_query = "SELECT my_column FROM my_database WHERE my_column IS NOT NULL";
That should do it.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: NULLS vs Empty result in PHP
am 23.09.2009 16:48:47 von Dan Shirah
--000e0cd5a2a6f8f88604743fcfc9
Content-Type: text/plain; charset=ISO-8859-1
>
> From reading the other responses to this thread, it seems that you want
> to
> "skip" or "exclude" rows in the results where my_column === null.
>
> If this is correct, why not do it in the SELECT statement to begin with?
>
> $my_query = "SELECT my_column FROM my_database WHERE my_column IS NOT
> NULL";
>
> That should do it.
>
I want to exclude the rows that might be NULL, "" (empty), or " "
(empty series of spaces)
From all of the input so far, it seems that using trim() on the variable and
then use empty() is the best way to pick all three types up.
--000e0cd5a2a6f8f88604743fcfc9--
Re: NULLS vs Empty result in PHP
am 23.09.2009 17:35:04 von Philip Thompson
On Sep 23, 2009, at 9:48 AM, Dan Shirah wrote:
>>
>> From reading the other responses to this thread, it seems that you
>> want
>> to
>> "skip" or "exclude" rows in the results where my_column === null.
>>
>> If this is correct, why not do it in the SELECT statement to begin
>> with?
>>
>> $my_query = "SELECT my_column FROM my_database WHERE my_column IS NOT
>> NULL";
>>
>> That should do it.
>>
>
> I want to exclude the rows that might be NULL, "" (empty), or
> " "
> (empty series of spaces)
>
> From all of the input so far, it seems that using trim() on the
> variable and
> then use empty() is the best way to pick all three types up.
I don't think you're using mysql, but your selected db may have a
similar option. I would do the work in the sql.
$sql = "SELECT * FROM `table` WHERE TRIM(`column`) <> '' AND `column`
IS NOT NULL";
$result = query($sql);
while ($row = fetch_row ($result)) {
echo "Not empty, multiple spaces or NULL!";
}
?>
So, if you have any extraneous spaces, they will be removed. It also
accounts for nulls. No work is required by PHP to determine the values
you need. Just use what it returns. This should work.
Hope this helps.
~Philip
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: NULLS vs Empty result in PHP
am 23.09.2009 18:16:00 von Phpster
On Wed, Sep 23, 2009 at 11:35 AM, Philip Thompson
wrote:
> On Sep 23, 2009, at 9:48 AM, Dan Shirah wrote:
>
>>>
>>> From reading the other responses to this thread, it seems that you want
>>> to
>>> "skip" or "exclude" rows in the results where my_column ===3D null.
>>>
>>> If this is correct, why not do it in the SELECT statement to begin with=
?
>>>
>>> $my_query =3D "SELECT my_column FROM my_database WHERE my_column IS NOT
>>> NULL";
>>>
>>> That should do it.
>>>
>>
>> I want to exclude the rows that might be NULL, "" (empty), or " =A0 =A0 =
=A0 =A0"
>> (empty series of spaces)
>>
>> From all of the input so far, it seems that using trim() on the variable
>> and
>> then use empty() is the best way to pick all three types up.
>
> I don't think you're using mysql, but your selected db may have a similar
> option. I would do the work in the sql.
>
>
> $sql =3D "SELECT * FROM `table` WHERE TRIM(`column`) <> '' AND `column` I=
S NOT
> NULL";
> $result =3D query($sql);
> while ($row =3D fetch_row ($result)) {
> =A0 =A0echo "Not empty, multiple spaces or NULL!";
> }
> ?>
>
> So, if you have any extraneous spaces, they will be removed. It also
> accounts for nulls. No work is required by PHP to determine the values yo=
u
> need. Just use what it returns. This should work.
>
> Hope this helps.
> ~Philip
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Another thought might be to convert the nulls into another value
select field1, field2, ISNULL(field3,'-') from table where ...
would convert the nulls into hyphens
--=20
Bastien
Cat, the other other white meat
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php