简体   繁体   English

如何使用PHP连接到SQL Server

[英]how to connect to sql server with php

I'm new in sql server.i have application that use MySQL and now i want to use sql server instead of MySQL in that application. 我是sql server的新手。我有使用MySQL的应用程序,现在我想在该应用程序中使用sql server而不是MySQL。 my php is: 我的PHP是:

<?php
$myServer = "localhost";
$myUser = "";
$myPass = "";
$myDB = "UNIVERSITY"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
 or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
 or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database
$query = "SELECT clgname";
$query .= "FROM dbo.clg ";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
if($numRows==0){
echo "false";
}
else{
echo "true";
}

//close the connection
mssql_close($dbhandle);
?>

i also remove the semi-colon before the 我还删除了分号之前

extension=php_mssql.dll

but i see error: 但我看到错误:

Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\1.php on line 8

please help. 请帮忙。 thanks. 谢谢。

Your PHP doesn't compile with mssql extension, just ask hosting provider to enable it if they support ,of if you have own server, just compile PHP with mssql extension --with-mssql=DIR where DIR is the FreeTDS install prefix. 您的PHP不使用mssql扩展名进行编译,如果托管服务器支持,则要求托管服务提供商启用它;如果您拥有自己的服务器,则仅使用mssql扩展名--with-mssql = DIR编译PHP,其中DIR是FreeTDS安装前缀。 And FreeTDS should be compiled using --enable-msdblib . FreeTDS应该使用--enable-msdblib进行编译。

You can use PDO to make this work. 您可以使用PDO来完成这项工作。 I use it with MSSQL and works great. 我将其与MSSQL一起使用,效果很好。 Example: 例:

try {
    $mssql = new PDO('mssql:host=localhost;dbname=UNIVERSITY', 'username', 'passwd');
    $sth = $mssql->prepare("SELECT * FROM STUDENTS");
    $sth->execute();
    $students_list = array();
    $students_list = $sth->fetchAll();
    $mssql = null;
} catch (PDOException $e) {
    $students_list = null;
}

EDIT: si-le is right, you also have to install the mssql module and add the line to php.ini for this to work 编辑:si-le是正确的,您还必须安装mssql模块并将行添加到php.ini中才能正常工作

Looks like the mssql extension isn't being enabled properly... 似乎未正确启用mssql扩展...

If you are running WAMP locally, then you may have a few extra steps to complete. 如果在本地运行WAMP,则可能需要完成一些额外的步骤。

I found a blog about this, and made my own here; 我找到了一个关于此的博客,并在这里建立了自己的博客;

http://pjgcreations.blogspot.co.uk/2013/01/enabling-ms-sql-extensions-in-wamp.html http://pjgcreations.blogspot.co.uk/2013/01/enabling-ms-sql-extensions-in-wamp.html

The basic idea is; 基本思想是:

  1. Download ntwdblib.dll: http://www.pjgcreations.co.uk/BlogAttachments/ntwdblib.DLL 下载ntwdblib.dll: http ://www.pjgcreations.co.uk/BlogAttachments/ntwdblib.DLL
  2. Click on the WAMP icon -> PHP -> PHP Extensions then check "php_mssql" and "php_pdo_mssql". 单击WAMP图标-> PHP-> PHP Extensions,然后检查“ php_mssql”和“ php_pdo_mssql”。 (Wamp will restart and give you few errors, ignore them) (Wamp将重新启动,并给您一些错误,请忽略它们)
  3. Restart WAMP one more time to ensure settings are saved. 再重启一次WAMP,以确保保存设置。
  4. Finally, place ntwdblib file in the two following directories: "wamp\\bin\\php\\php5.3.1" (or the PHP Directory relating to the version you are using) and "wamp\\bin\\apache\\apache2.2.11\\bin" (Or the Apache Directory relating to the version you are using) 最后,将ntwdblib文件放在以下两个目录中:“ wamp \\ bin \\ php \\ php5.3.1”(或与您使用的版本有关的PHP目录)和“ wamp \\ bin \\ apache \\ apache2.2.11 \\ bin”(或与您使用的版本有关的Apache目录)
  5. Restart wampserver, and you're finished! 重新启动wampserver,您就完成了!

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM