0

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>'

Edited by RickL66: fixed snippet

1
Contributor
2
Replies
4
Views
5 Years
Discussion Span
Last Post by RickL66
0

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.

0

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;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.