תשובה sqlsrv PDO not executing queries

  • Friday, June 17, 2011 5:24 PM
     
      Has Code

    I am following the blog post found here 

    http://blogs.msdn.com/b/sqlphp/archive/2010/08/04/microsoft-drivers-for-php-for-sql-server-2-0-released.aspx 

     

    to use PDO with PHP to access the PHP database. the sample code provided in the blog,

    $serverName = "(local)\sqlexpress"; 
    
     /* Connect to SQL Server using Windows Authentication. */ 
     $conn = new PDO("sqlsrv:server=$serverName;Database=AdventureWorks","",""); 
    
     /* Get products by querying against the product name.*/ 
     $tsql = "SELECT ProductID, Name, Color, Size, ListPrice FROM Production.Product"; 
    
     /* Execute the query. */ 
     $getProducts = $conn->query( $tsql ); 
    
     
     /* Loop thru recordset and display each record. */ 
     while( $row = $getProducts->fetch( PDO::FETCH_ASSOC ) ) 
     { 
      print_r( $row ); 
     } 
    
     /* Free the statement and connection resource. */ 
     $getProducts = NULL; 
     $conn = NULL; 
    


    does not seem to work. the $getProducts variable is not a proper value, and we are failing when trying to call the fetch() function farther down. results in the following error  

     Call to a member function fetch() on a non-object in (site)   

     

    Other similar queries that should work also fail to go through. anyone know what the problem might be?

All Replies

  • Monday, June 20, 2011 5:25 AM
     
     Answered

    Hi weranidr,

    Does the database exist on the SQL Server Express instance? Can you connect to the database using SQL Server Management Studio? 

    I think you can refer to the following link to catch the error message: http://msdn.microsoft.com/en-US/library/ff754357(v=SQL.90).aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Get or Request Code Sample from Microsoft
    If you have any feedback, please tell us.
    • Marked As Answer by KJian_ Monday, June 27, 2011 6:23 AM
    •  
  • Monday, June 27, 2011 8:41 PM
     
     
    I solved this problem. I can't remember exactly what the problem was, but all I had to do was enable $conn->query to send back exceptions. It told me exactly what the problem was.