•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,584 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,585 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1328 | Replies: 3
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hi! I'm a bit of a beginner at PHP, and I'm having trouble comparing two strings in a conditional statement. Both of the variables are strings, but one of them them is assigned from a MSSQL database. In this database, it of the unicode varchar data type. It doesn't seem like this should make a difference, but for some reason, whenever I try to compare the two dates, I get a false return value, even when the dates are equivalent.
Here's my code:
Thanks,
David
Here's my code:
//Set database access variables
$host ='mssql.library.uiuc.edu';
$user = 'BEL_webuser';
$pass ='NeUn72L';
$db = 'BEL';
//Open connection
$connection = mssql_connect($host, $user, $pass) or die("Unable to connect");
//Select database
mssql_select_db($db) or die("Unable to select database");
// create query
//$query = "SELECT computer, time, netid FROM bloomberg WHERE(date=N'$date') AND (computer=N'$comp')";
$query = "SELECT * FROM bloomberg";
// execute query
$result = mssql_query($query) or die ("Error in query");
$date1 = '11-05-2007';
for($i = 0; $i < mssql_num_rows($result); $i++){
$row = mssql_fetch_array($result);
$date2 = $row['date'];
if($date1 == $date2){
$dateflag = 1;
}
else {
$dateflag = 0;
}
echo $dateflag;
}
Thanks,
David
•
•
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation:
Rep Power: 2
Solved Threads: 5
•
•
•
•
Hi! I'm a bit of a beginner at PHP, and I'm having trouble comparing two strings in a conditional statement. Both of the variables are strings, but one of them them is assigned from a MSSQL database. In this database, it of the unicode varchar data type. It doesn't seem like this should make a difference, but for some reason, whenever I try to compare the two dates, I get a false return value, even when the dates are equivalent.
Here's my code:
//Set database access variables $host ='mssql.library.uiuc.edu'; $user = 'BEL_webuser'; $pass ='NeUn72L'; $db = 'BEL'; //Open connection $connection = mssql_connect($host, $user, $pass) or die("Unable to connect"); //Select database mssql_select_db($db) or die("Unable to select database"); // create query //$query = "SELECT computer, time, netid FROM bloomberg WHERE(date=N'$date') AND (computer=N'$comp')"; $query = "SELECT * FROM bloomberg"; // execute query $result = mssql_query($query) or die ("Error in query"); $date1 = '11-05-2007'; for($i = 0; $i < mssql_num_rows($result); $i++){ $row = mssql_fetch_array($result); $date2 = $row['date']; if($date1 == $date2){ $dateflag = 1; } else { $dateflag = 0; } echo $dateflag; }
Thanks,
David
SQL Server stores the datetime in a certain manner. The variable $date1 that you have set looks to be in the incorrect format.
Take a look at this to get a bit more information. http://www.karaszi.com/SQLServer/inf...#DtInSqlServer
I keep hitting "escape", but I'm still here!!!!
:}
:}
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
SQL Server stores the datetime in a certain manner. The variable $date1 that you have set looks to be in the incorrect format.
Take a look at this to get a bit more information. http://www.karaszi.com/SQLServer/inf...#DtInSqlServer
•
•
•
•
if u wanna compare two dates in that way you will always get "0", coz sql server stores the dates like
dd/mm/yyyy hh:mm:ss ms etc..
u should change your query as between $date1 and $date2 or reduce $date1 n $date2 to dd/mm/yyyy hh/mm then u can compare them..
Thank you both for the help. I've stored my date as a string, though, using the nvarchar data type.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Help in terminating a program (C++)
- I need help again (C++)
- Comparing Strings (Assembly)
- problem in concatenation of 2 strings (C)
- Error comparing strings from arrays (PHP)
- Comparing Strings in C# (C#)
- problem with Chars and Strings (C++)
- Having trouble with comparing strings and arrays (Java)
- comparing two strings with linear search.. (Java)
Other Threads in the PHP Forum
- Previous Thread: Smart URL's
- Next Thread: Adding x amount of days to a specific date?


Linear Mode