473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to use php date in mysql query

30
Hi

I need to run a query based on a php date variable.

Expand|Select|Wrap|Line Numbers
  1. $add2days=strtotime(date("Ymd",$MyToday). "+2 day");
  2. echo".....date(add2days)=".date('Y-m-d',$add2days)
  3.  
  4. $query2 = "SELECT `daydate` FROM publicholiday WHERE `daydate`=`date('Y-m-d',$add2days)`";
  5. $aresult = $dbLink->query($query2);  
  6.     //$resultrow = $aresult->fetch_row();
  7.     //echo"sunday holiday row is".$resultrow[0];
  8.     if($aresult)
  9.      {.... }
  10.  
  11.  
if($aresult) is false when it should actually be true for the date I am sending

Thanks for the help!!
Sep 27 '10 #1

✓ answered by Dormilich

How should I do it then ?
for a start, by not putting PHP functions inside a string. (this only works in some special cases)

this would make way more sense:
Expand|Select|Wrap|Line Numbers
  1. $query2 = "SELECT `daydate` FROM `publicholiday` WHERE `daydate` = " . date('Y-m-d',$add2days);

6 2149
Dormilich
8,658 Expert Mod 8TB
if($aresult) is false when it should actually be true for the date I am sending
how do you know, that it *should* be true? actually, your query should either return noting or cause an error.

PS. the MySQL DATE() function expects only one parameter.
Sep 27 '10 #2
Tinus
30
Hi

well, I have a mysql table in which all public holidays are stored.
A date is entered into an html form. Then my php tests if this day is a holiday.
In this specific section; it looks if the public holiday is on a sunday. If it is monday will be a holiday .

What I am actually asking is if the following will return a result or will it return an error, or empty result.
Expand|Select|Wrap|Line Numbers
  1. $query2 = "SELECT `daydate` FROM publicholiday WHERE `daydate`=`date('Y-m-d',$add2days)`"; 
  2. $aresult = $dbLink->query($query2);   
  3.  
  4.  
The date is in the mysql table so if done correctly it should return a result.
Sep 27 '10 #3
Dormilich
8,658 Expert Mod 8TB
that’s the part (in bold), that's not done correctly. thus you *should* get an error.
Expand|Select|Wrap|Line Numbers
  1. $query2 = "SELECT `daydate` FROM publicholiday WHERE `daydate`=`date('Y-m-d',$add2days)`"; 
Sep 27 '10 #4
Tinus
30
Ok,thanks

How should I do it then ?

Expand|Select|Wrap|Line Numbers
  1. echo $_POST["inputdate"];
  2. $indate=$_POST["inputdate"];
  3. echo " indate is".$indate;
  4. if (isset($_POST["inputdate"])){ 
  5. $MyToday=date("Ymd");
  6. $MyToday=strtotime($indate);}
  7. else{
  8. $MyToday=$today;}
  9.  
  10. if((date("D",$MyToday))=='Fri') 
  11.    {
  12.        $add2days=strtotime(date("Ymd",$MyToday). "+2 day");
  13.     echo".....date(add2days)=".date('Y-m-d',$add2days);
  14.    $query2 = "SELECT daydate FROM publicholiday WHERE daydate=date('Y-m-d',$add2days)";
  15.    //(SELECT DATE_FORMAT( (SELECT DATE_ADD(`$Mytoday`,INTERVAL 2 DAY ) ) ,'%Y-%m-%d'))";
  16.     $aresult = $dbLink->query($query2);  
  17.     //$resultrow = $aresult->fetch_row();
  18.     //echo"sunday holiday row is".$resultrow[0];
  19.     if($aresult)
  20.      { //Vrydag en vakansiedag val op sondag
  21.        echo "public holiday on sunday so monday will be holiday";
  22.        //doen f,s,s,m
  23.        exec("C:/aptana/xampp-win32-1.7.3/xampp/htdocs/sas_prog >sas_prog.out");
  24.        exec("Psexec.exe -i -d  sas.exe");   
  25.         }
  26.  
Sep 27 '10 #5
Dormilich
8,658 Expert Mod 8TB
How should I do it then ?
for a start, by not putting PHP functions inside a string. (this only works in some special cases)

this would make way more sense:
Expand|Select|Wrap|Line Numbers
  1. $query2 = "SELECT `daydate` FROM `publicholiday` WHERE `daydate` = " . date('Y-m-d',$add2days);
Sep 27 '10 #6
Tinus
30
:) Thank you for the help.It's working now.
Sep 27 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: joemyre | last post by:
Hi everyone, What I'm trying to do is take php variables i got from user input, and pass them as the MySQL query terms. $query = "select * from ident where ".$searchtype1."=".$searchterm1."";...
2
by: JDJones | last post by:
Using PHP and MySQL. Trying to put a list of categories into a drop down select option of a form like: <form name="form" action="<? print $_SERVER?>" method="get"> <select name="subject">...
0
by: Murphy | last post by:
I am currently migrating a db from SQL Server & MySQL and ? (front end yet to be decided upon) As I familiarise myself with MySQL populating tables etc can be quite clumbersome. After reading the...
2
by: laredotornado | last post by:
Hello, Using PHP 4 and MySQL 4, how would I write a function such that it takes a valid MySQL query, and a column on which to order the query, and return a new query ordered by that column? ...
0
by: taras.di | last post by:
Hi everyone, I've come across an extremely strange problem. The exact same query in both mysql command line client, and mysql query browser gives entirely different results. I was hoping someone...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
3
by: runvik | last post by:
Hi! I wonder how I, in the best way, can trim/format/strip down a MySQL query so that I can present the values of it in a confirm window? Example: INSERT INTO `test` ( `id` , `car` ,...
2
by: stmfc | last post by:
i have a problem with user creation for mysql db i think the problem stems from string style. GRANT ALL ON newDB.* TO 'test'@'localhost' IDENTIFIED BY 'test'; i am...
1
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.