I have an application that is writen in PHP and connects to a MS SQL Server 2008 Database. I'm using ADO recordsets. There are cases where specific IDs and years in the table returns nothing. How do I test for when there are no results? I searched and most of the results are when using MySQL but I'm not using that. Here's a sample of my code:

$myServer = "localhost";
$myUser = "user";
$myPass = "password";
$myDB = "myDatabase";

//create an instance of the  ADO connection object
$conn = new COM ("ADODB.Connection")
  or die("Cannot start ADO");
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

//define connection string, specify database driver
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
$conn->open($connStr); //Open the connection to the database

//declare the SQL statement that will query the database
$query = "Select from tblName where id='10' and year='2010'";

//execute the SQL statement and return records
$rs = $conn->execute($query); 

if(!is_null($rs->Fields[0]))
    echo 'We have something here <br>'
else
    echo 'We have a null <br>'

I can't seem to edit my own post, but I see I missed a * in "select * from" - but this is just a code snippet example, and my real code does have the * in it.

I guess I answered my own question. PHP in IIS using ADODB does not return recordcounts so I have to query it myself. This is how I did it:

 //declare the SQL statement that will query the database
$query = "Select Count(*) as Recordcount from tblName where id='10' and year='2010'";
//execute the SQL statement and return records
$rs = $conn->execute($query);

Then I get the count by doing this:

$Count = $rs->Fields("RecordCount")->value;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.