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

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/info_datetime.asp#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..

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/info_datetime.asp#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.

This article has been dead for over six months. Start a new discussion instead.