Re: PHP Mysql query data conversion newbie

From: Date: Thu, 10 May 2001 02:32:24 +0000
Subject: Re: PHP Mysql query data conversion newbie
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-52219@lists.php.net to get a copy of this message
On Sat, 28 Apr 2001, Andras Kende wrote: > Hello, > > I pull some data from mysql with the php code below. > On the date field if there is no date on mysql it displays : 0000-00-00 > 00:00:00 > > I would like to change this 0000-00-00 00:00:00 to "no date" for example.. hmm... mysql can do this, works like this: IF(EXPR,TRUE,FALSE): mysql> select IF(UNIX_TIMESTAMP(lastmod)<=0,"NoDate",lastmod) as date from links; +---------------------+ | date | +---------------------+ | 2001-04-22 08:04:07 | | 2001-04-22 08:04:10 | | 2001-04-22 08:03:45 | | 2001-04-22 08:03:42 | | 2001-04-22 08:03:41 | ... | 2001-04-22 08:03:45 | | 2001-04-22 08:04:05 | | 2001-04-22 08:03:43 | | NoDate | (normally 0000-00-00 00:00:00) | 2001-04-22 08:04:06 | | 2001-04-22 08:03:56 | | 2001-04-22 08:04:08 | | 2001-04-22 12:57:27 | | 2001-04-27 21:26:36 | | 2001-05-07 22:35:45 | +---------------------+ 28 rows in set (0.00 sec) > Or if the cel is empty to " " (because otherwise the tableborders are messed > up) If by empty you mean NULL, you could probably do similar as above: mysql> select IF(lastchk IS NULL,"&nbsp;",lastchk) from links; +--------------------------------------+ | IF(lastchk IS NULL,"&nbsp;",lastchk) | +--------------------------------------+ | 24 | | 9 | | &nbsp; | | 8 | ... | &nbsp; | | 6 | | 1 | | &nbsp; | | &nbsp; | | &nbsp; | | &nbsp; | +--------------------------------------+ 28 rows in set (0.01 sec) ... the &nbsp; will hold the table cell in html. > > I tried to put an if statement inside the while {} but i cannot figured > out... yep you could do this too. I don't know which is faster as i have not been able to test either under high enough load to make a difference :( i prefer to let mysql do all the work it can because i figure the web server is busy enough will all kind of regular html requests. good luck... hope this helped. Bill

« previous php.general (#52219) next »