Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 02-06-2005, 12:45 PM   #1 (permalink)
Wysocki
Recruit
 
Join Date: Jan 2005
Location: Covina, CA
Posts: 23
Wysocki is on a distinguished road
How to test for empty date from MySql call?

Code:
$result=mysql_query("SELECT * FROM sk_recvitem WHERE 1");
$shipdate=mysql_result($result,1,"shipdate");
if ($shipdate = "0000-00-00") $shipdate="NONE";
I have the above code accessing a MySql database to return a date field so I can place it on my webpage. If the date field has not been set, the value in $shipdate shows up as 0000-00-00 but I'd like the field to show NONE. The above IF test ALWAYS evaluates to true though. I can't use empty($shipdate) because it appears to contain 10 characters. How should I test for this?
Wysocki is offline   Reply With Quote
Old 02-06-2005, 12:56 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
Quote:
Code:
if ($shipdate = "0000-00-00") $shipdate="NONE";
Should be:
Code:
if ($shipdate == "0000-00-00") $shipdate="NONE";
or
Code:
if ($shipdate === "0000-00-00") $shipdate="NONE";
in your case it's if $shipdate can be assigned the value "000-00-00" which will allways be true.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-06-2005, 01:00 PM   #3 (permalink)
Wysocki
Recruit
 
Join Date: Jan 2005
Location: Covina, CA
Posts: 23
Wysocki is on a distinguished road
Duh!! Thanks for the quick reply. I'm new to php so I keep confusing it with syntax from all the other languages I've worked with.
Wysocki is offline   Reply With Quote
Old 02-06-2005, 01:17 PM   #4 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Redhead has you covered there, but I'll be a syntax nazi for a bit..

- It's always good practice to put your literal first in a comparison to avoid this very scenario.
eg:
PHP Code:
if ('0000-00-00' $shipdate) {
   
$shipdate 'NONE';

So even if you use = instead of == it'll evaluate as false and you'll find the problem earlier. Plus, if you have a large conditional statement, your literal will be in at the front and easier to read.

- One-liners are never good for code maintainability.
Not very clear:
PHP Code:
if ('0000-00-00' == $shipdate)
   
$shipdate 'NONE';
   
func1();
   
func2();
   
//etc.. 

much better:
PHP Code:
if ('0000-00-00' == $shipdate) {
   
$shipdate 'NONE';
}
   
func1();
   
func2();
   
//etc.. 
-r
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembelly code register addressing toblerone Assembly 5 05-22-2006 02:53 AM
MySQL Replication / Failover idx Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 2 02-23-2005 07:22 PM
and on to mysql .. sde Linux / BSD / OS X 2 01-18-2003 07:39 PM


All times are GMT -8. The time now is 09:54 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting