[英]How to connect php webservice to 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上给出致命错误。
致命错误:在第11行的service.php中调用未定义的函数mssql_connect()
那么如何将php web服务与sql server 2008 r2连接起来呢?
提前致谢...
您将需要从Microsoft下载并安装sqlsrv扩展名。 在此处查找一个选项: http : //www.microsoft.com/zh-cn/download/details.aspx?id=20098
请注意,该扩展名不再称为“ mssql”,而是现在为“ sqlsrv”,因此您将要使用sqlsrv_connect()
等。
得到答案..............
<?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;
?>
使用php成功连接到sql server 2008 r2数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.