php question

by 5 replies
6
I'm trying to exclude some entries in a db that have the date set in the db some entries have under the SOLD colum 0000-00-00 and some have 2012-12-23 so if there is a date there means the vehicle is sold and if it's zero means it's still here..

how can I exclude them that have a date

I'm using SELECT * FROM `cars` WHERE `model`="$userchoice" AND `sold` = "0000-00-00"

is this the best way to do it - or?
#programming #php #question
  • SELECT * FROM `cars` WHERE 'model' = '$userchoice' AND 'sold = '0000-00-00'

    looks fine, except for the backticks your using for column names.
  • In addition, you may wish to use MySQL Workbench to test your SQL. It is free and can be found MySQL :: Download MySQL Workbench.
  • [DELETED]
  • SELECT * FROM `cars` WHERE `model`='$userchoice' AND `sold` = '0000-00-00'

    I corrected your syntax but other than that it is good. If you've set this up in PHP you want

    $query = "SELECT * FROM `cars` WHERE `model`='{$userchoice}' AND `sold` = '0000-00-00'";
  • Thanks this is great post.It give me some good tips about PHP.I want to learn more about PHP.
  • While that works, that is not exactly best practices, as well as works for that exact case. It might be better off to convert the date to unix timestamp, and work with it that way. The syntax for converting a string to unix timestamp is...
    UNIX_TIMESTAMP

    and you can read a brief tutorial on it <a href="http://stackoverflow.com/questions/3413559/mysql-convert-mm-dd-yy-to-unix-timestamp">here</a>

    Also, another practice is to grab all of the dates, and then use strtotime in php and manipulate the dates that way.

    so in your instance, you would have done something like...

    I'm using SELECT * FROM `cars` WHERE `model`= "$userchoice" AND FROM_UNIXTIME(`sold`) > 0

Next Topics on Trending Feed

  • 6

    I'm trying to exclude some entries in a db that have the date set in the db some entries have under the SOLD colum 0000-00-00 and some have 2012-12-23 so if there is a date there means the vehicle is sold and if it's zero means it's still here.. how can I exclude them that have a date