Question

I am trying to make a very simple query to a SQL database using PHP, but no matter what I do, it seems it can't pull any proper results. The following is the code I have written:

$loDate = '2013-10-01';
$hiDate = '2015-01-01';
$queryStr = "select TimeCol from dbo.SUBPUMP where TimeCol between " . $loDate . " and " . $hiDate;
$conn = sqlsrv_connect($serverName, $conInfo);
$result = sqlsrv_query($conn, $queryStr);

while ($row = sqlsrv_fetch_array($result)) {
    // This never prints.
    echo "row:<br>"; var_dump($row); echo "<br><br>";
}

echo "<br><br><br>";

// Prints "conInfo: resource(2) of type (SQL Server Connection)"
echo "conn: "; var_dump($conn); echo "<br><br>";

// Prints "result: resource(3) of type (SQL Server Statement)"
echo "result: "; var_dump($result); echo "<br><br>";

// Prints "row: NULL"
echo "row: "; var_dump($row); echo "<br><br>";

What I am confused on is how it can establish a proper connection to the database.

Since $conn and $result do not print any errors, but yet do not seem to actually fetch any data for me.

So far, my best guess is that since I am attempting to grab the data from a database through a VPN, and thus might only have a tenuous connection, that the sqlsrv_connect() will attempt to try many times to connect to the database until it receives a single good connection. While the sqlsrv_query() will only attempt this once, and thus fails to gather any data.

Does anyone else have any ideas as to what might be going wrong here?

Was it helpful?

Solution 3

Even your result is equal to null, $result returns this

// Prints "result: resource(.) of type (SQL Server Statement)"
echo "result: "; var_dump($result); echo "<br><br>";

Your query may returns null, because of your date format. Did you try this query without dates

$queryStr = "select TimeCol from dbo.SUBPUMP";

OTHER TIPS

The documentation, for sqlsrv_fetch_array, states:

Returns an array on success, NULL if there are no more rows to return, and FALSE if an error occurs.

A NULL result can therefore only mean that there are 0 results from your query. So while ($row = sqlsrv_fetch_array($result)) will never loop.

You could handle this with a conditional check with sqlsrv_has_rows(). This returns a boolean true if there are more than 0 rows.

For example:

if (sqlsrv_has_rows($result)) {
  while ($row = sqlsrv_fetch_array($result)) {
    echo "row:<br>"; var_dump($row); echo "<br><br>";
  }
} else {
  echo "<br/>No Results were found."; 
}

Since you are referring to date column, try setting "ReturnDatesAsStrings" option in connection option(http://msdn.microsoft.com/en-us/library/ff628167.aspx).

Sample connection:-

$connection['UID'] = $username;
$connection['PWD'] = $password;
$connection['Database'] = $database_name;
$connection['ReturnDatesAsStrings'] = true;
$connection['ConnectionPooling'] = false;
$connection['CharacterSet'] = 'UTF-8';

if (!empty($port)) {
  $hostname = $hostname.', '.$port;
}

$conn = sqlsrv_connect($hostname, $connection);

Also try setting cursor type to "SQLSRV_CURSOR_STATIC" in sql_query function (http://msdn.microsoft.com/en-us/library/hh487160.aspx)

Sample Query:-

$result_id = sqlsrv_query($conn, $queryStr, null, array(
                'Scrollable' => SQLSRV_CURSOR_STATIC,
                'SendStreamParamsAtExec'=> true
            ));


while($row = sqlsrv_fetch_object($result_id)) {

}

I'm a little surprised this has gone so long without an answer. The problem here is very simple: The dates don't have single quotes around them.

$loDate = '2013-10-01';
$hiDate = '2015-01-01';
$queryStr = "select TimeCol from dbo.SUBPUMP where TimeCol between '" . $loDate . "' and '" . $hiDate . "'";

SQL Server requires date strings to be quoted to interpret them as dates.

Also, probably worth noting that you should be using a prepared statement. That is safer and doesn't require you to quote your string.

I know this is an old answer, but I was having he same problem with it today.

Turns out I was using an old version of PHP. Updating to a newer version fixed this for me. In my case it was going from version 5.6 to 7.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top