We help IT Professionals succeed at work.

PHP select query on MS SQL SERVER 12

My client has a MS SQL Server 12 database table with a column - binary(8000)
It is populated with HashBytes('SHA1', '[concatenation of nvarchar entries of several other columns]').
Sample value is: 0x6C44CFB1C32C6E4ECDEBAC863FD5EA345681AC18

Using PHP 5.6.6, I collect the needed info to duplicate the concatenation described above and use the info to try to convert to an encryption to match the MS SQL SERVER 12 entry.
Nothing has worked yet  - I've tried many variations of a select statement like this one:
     $result="SELECT WebSiteEncryptedKey FROM Web.data where WebSiteEncryptedKey=HashBytes('SHA1',N'$webencryptedkey')";
     echo $result;
     echo "<br />";
     $rows = $db->query($result);
     if(!$rows)
     {
          die($db->errorInfo());
     } else {
          echo $rows; // line 102
     }
/*
RESULTS:
SELECT WebSiteEncryptedKey FROM Web.data where WebSiteEncryptedKey=HashBytes('SHA1',N'CURVATURESFP-10G-LR-CURV1154263')

Catchable fatal error: Object of class PDOStatement could not be converted to string in D:\inetpub\wwwroot\Registration.php on line 102
*/

Any help with doing this the correct way will be appreciated very much.
Thank you,
softexecware
Comment
Watch Question

Brian TaoOwner
Top Expert 2014
Commented:
2 things:
1) you may want to cast the WebSiteEncryptedKey column to nvarchar(max) in the comparison
2) the error says $row is a PDOStatement (which is an associate result set after query executed) and cannot use echo to output such data type.
IT Engineer
Distinguished Expert 2017
Commented:
You can't compare Binaries values. You'll need to perform a conversion to VARCHAR so you can compare the values. Maybe something like:
SELECT WebSiteEncryptedKey 
FROM Web.data 
WHERE CAST(WebSiteEncryptedKey AS NVARCHAR)=CAST(HashBytes('SHA1',N'CURVATURESFP-10G-LR-CURV1154263') AS NVARCHAR)

Open in new window

Jerry KrogerCTO - Founder

Author

Commented:
Thank you Brian, and thank you Vitor.
Both comments were spot on.
Vitor, your detailed example was just what I needed to understand  the concept.

Again thanks to each of you.
softexecware