2013-12-09 4 views
0

Мы разрабатываем PHP веб-сервиса, и наша база данных на SQL Server 2008 R2 строкаКак подключить PHP WebService к SQL Server 2008 R2

/SQL * подключение */

 $link = mssql_connect('localhost','usrname','pass') or die('Cannot connect to the DB');  
    mssql_select_db('Data Source=PC-NAME;Initial Catalog=DBNAME.MDF;Persist Security Info=True',$link) or die('Cannot select the DB'); 

     /* grab the posts from the db */ 

     echo $query = "SELECT * FROM add_product WHERE prod_pos = '".$_GET['prod_pos']."'"; 
     $result = mssql_query($query,$link) or die('Errant query: '.$query); 

дает роковую ошибку на mssql_connect.

Фатальная ошибка: Вызов неопределенной функции mssql_connect() в service.php строке 11

Так как подключить PHP веб-сервиса с SQL Server 2008 R2 ???

Спасибо заранее ...

ответ

0

Вам нужно будет скачать и установить расширение SQLSRV от Microsoft. Посмотрите здесь один из вариантов: http://www.microsoft.com/en-us/download/details.aspx?id=20098

Обратите внимание, что расширение больше не называется «MSSQL», но вместо этого в настоящее время «SQLSRV», так что вы будете использовать sqlsrv_connect() и т.д.

0

получил ответ ... ...........

<?php 
     $number_of_posts = $_GET['prod_pos']; 

     $format = 'json'; 

     /* connect to the db */ 

     $serverName = "(local)"; 

     /* Get UID and PWD from application-specific files. */ 
     $uid = "usrname of sql server 2008"; 
     $pwd = "password"; 
     $connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"db name"); 

     /* Connect using SQL Server Authentication. */ 
     $conn = sqlsrv_connect($serverName, $connectionInfo); 
     if($conn === false) 
     { 
      echo "Unable to connect.</br>"; 
      die(print_r(sqlsrv_errors(), true)); 
     } 


     /* Query SQL Server for the login of the user accessing the database. */ 

     $tsql = "SELECT * FROM add_product WHERE prod_pos='".$number_of_posts."'"; 
     $stmt = sqlsrv_query($conn, $tsql); 
     if($stmt === false) 
     { 
      echo "Error in executing query.</br>"; 
      die(print_r(sqlsrv_errors(), true)); 
     } 


     /* create one master array of the records */ 

     $posts = array(); 
     while($post = sqlsrv_fetch_array($stmt)) 
     { 
       $posts[] = array('post'=>$post); 
     } 


     /* output in necessary format */ 

     if($format == 'json') 
     { 
      header('Content-type: application/json'); 
      echo json_encode(array('posts'=>$posts)); 
     } 

     /* disconnect from the db */ 

     $stmt = null; 
     $conn = null; 

    ?> 

Успешное подключение к SQL Server 2008 базы данных r2 с использованием PHP

Смежные вопросы