Viewed   596 times

I am trying to get the number of rows returned in a query. The while loop looping through the results works, but for some reason the sqlsrv_num_rows does not return any value:

$result = "SELECT * from dtable WHERE id2 = 'apple'";
$query = sqlsrv_query($conn, $result);

$row_count = sqlsrv_num_rows($query);
echo $row_count;

while($row = sqlsrv_fetch_array($query))
{
      echo 'yes';
}

Thanks.

 Answers

3

It is because sqlsrv_query() uses SQLSRV_CURSOR_FORWARD cursor type by default. However, in order to get a result from sqlsrv_num_rows(), you should choose one of these cursor types below:

  • SQLSRV_CURSOR_STATIC
  • SQLSRV_CURSOR_KEYSET
  • SQLSRV_CURSOR_CLIENT_BUFFERED

For more information, check: Cursor Types (SQLSRV Driver)

In conclusion, if you use your query like:

$query = sqlsrv_query($conn, $result, array(), array( "Scrollable" => 'static' ));

you will get result in:

$row_count = sqlsrv_num_rows($query);
Sunday, December 11, 2022
2

First if I'm not wrong you are storing sqlsrv_connect result into $conn and this result isn't a class obj its a resource, so remove $db->conn

This example, will connect, then fetch if there are resources returned from sqlsrv_query

$conn_array = array (
    "UID" => "sa",
    "PWD" => "root",
    "Database" => "nih_bw",
);
$conn = sqlsrv_connect('BILAL', $conn_array);
if ($conn){
    echo "connected";
    if(($result = sqlsrv_query($conn,"SELECT * FROM routines")) !== false){
        while( $obj = sqlsrv_fetch_object( $result )) {
              echo $obj->colName.'<br />';
        }
    }
}else{
    die(print_r(sqlsrv_errors(), true));
}
Friday, November 18, 2022
 
mmgp
 
1

There's an unofficial driver. Not sure if that will pass PCI.

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e1d37219-88a3-46b2-a421-73bfa33fe433/unofficial-php-55-drivers-x86

Sunday, September 25, 2022
 
2

In this line:

getSales(division);

You are discarding the return value, you need to assign it to a variable:

sales1 = getSales(division);
Saturday, November 5, 2022
 
aaron_k
 
5

Hi I raised this issue to facebook developer support and the issue is that I have not implemented the correct delegate methods for iOS10. and you can find the correct delegate implementation here

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


public func application(_ application: UIApplication,  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.

return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

 public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

return FBSDKApplicationDelegate.sharedInstance().application(
  app,
  open: url as URL!,
  sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
  annotation: options[UIApplicationOpenURLOptionsKey.annotation]
)
}

public func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(
  application,
  open: url as URL!,
  sourceApplication: sourceApplication,
  annotation: annotation)
}
}
Friday, December 23, 2022
 
tekhne
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :