User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

problem comparing strings

  #1  
Nov 5th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: problem comparing strings

  #2  
Nov 7th, 2007
Originally Posted by daver40 View Post
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!!!!
:}
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: problem comparing strings

  #3  
Nov 7th, 2007
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..
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

Re: problem comparing strings

  #4  
Nov 8th, 2007
Originally Posted by JeniF View Post
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

Originally Posted by fatihpiristine View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC