Connecting to SQL Server in Php - Extension Err
<html>
<head>
<title>Connecting </title>
</head>
<body>
<?php
$host = "*.*.*.*";
$username = "xxx";
$password = "xxx";
$db_name = "xxx";
$db = mssql_connect($host, $username,$password)
or die("Couldn't Connect");
$selected = mssql_select_db($db_name, $db)
or die("Couldn't open database");
?>
</body>
</html>
My error message is:
Fatal error: Call to undefined function mssql_connect() in C:\wamp\www\php\dbase.php on line 12
I am using WampServer 2.0 on Php 5.3.0
When I check the extensions, php_mssql is Checked. I also checked the php.ini file to make sure it is not commented out.
I have my file dbase.php saved in C:\wamp\www\php. I have tried stopping the service, closing everything, and running it again. I know the problem is that the extension file is not being included somehow.
The below is copied from my php.ini file. Note I made all http = /http to avoid posting Links.
;;;;;;;;;;;;;;;;;;;;;;;;; ; Paths and Directories ; ;;;;;;;;;;;;;;;;;;;;;;;;;
; UNIX: "/path1:/path2" ;include_path = ".:/php/includes" ; Windows: "\path1;\path2" include_path = "C:\wamp\bin\php\php5.3.0\ext" ; ; PHP's default setting for include_path is ".;/path/to/php/pear" ; /http://php.net/include-path
; The root of the PHP pages, used only if nonempty. ; if PHP was not compiled with FORCE_REDIRECT, you SHOULD set doc_root ; if you are running php as a CGI under any web server (other than IIS) ; see documentation for security issues. The alternate is to use the ; cgi.force_redirect configuration below ; /http://php.net/doc-root doc_root =
; The directory under which PHP opens the script using /~username used only ; if nonempty. ; /http://php.net/user-dir user_dir =
; Directory in which the loadable extensions (modules) reside. ; /http://php.net/extension-dir ; extension_dir = "./" ; On windows: ; extension_dir = "ext" extension_dir = "c:/wamp/bin/php/php5.3.0/ext/"
; Whether or not to enable the dl() function. The dl() function does NOT work ; properly in multithreaded servers, such as IIS or Zeus, and is automatically ; disabled on them. ; /http://php.net/enable-dl enable_dl = Off
; cgi.force_redirect is necessary to provide security running PHP as a CGI under ; most web servers. Left undefined, PHP turns this on by default. You can ; turn it off here AT YOUR OWN RISK ; You CAN safely turn this off for IIS, in fact, you MUST. ; /http://php.net/cgi.force-redirect ;cgi.force_redirect = 1
; if cgi.nph is enabled it will force cgi to always sent Status: 200 with ; every request. PHP's default behavior is to disable this feature. ;cgi.nph = 1
; if cgi.force_redirect is turned on, and you are not running under Apache or Netscape ; (iPlanet) web servers, you MAY need to set 开发者_开发问答an environment variable name that PHP ; will look for to know it is OK to continue execution. Setting this variable MAY ; cause security issues, KNOW WHAT YOU ARE DOING FIRST. ; /http://php.net/cgi.redirect-status-env ;cgi.redirect_status_env = ;
; cgi.fix_pathinfo provides real PATH_INFO/PATH_TRANSLATED support for CGI. PHP's ; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok ; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting ; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting ; of zero causes PHP to behave as before. Default is 1. You should fix your scripts ; to use SCRIPT_FILENAME rather than PATH_TRANSLATED. ; /http://php.net/cgi.fix-pathinfo ;cgi.fix_pathinfo=1
; FastCGI under IIS (on WINNT based OS) supports the ability to impersonate ; security tokens of the calling client. This allows IIS to define the ; security context that the request runs under. mod_fastcgi under Apache ; does not currently support this feature (03/17/2002) ; Set to 1 if running under IIS. Default is zero. ; /http://php.net/fastcgi.impersonate ;fastcgi.impersonate = 1;
; Disable logging through FastCGI connection. PHP's default behavior is to enable ; this feature. ;fastcgi.logging = 0
; cgi.rfc2616_headers configuration option tells PHP what type of headers to ; use when sending HTTP response code. If it's set 0 PHP sends Status: header that ; is supported by Apache. When this option is set to 1 PHP will send ; RFC2616 compliant header. ; Default is zero. ; /http://php.net/cgi.rfc2616-headers ;cgi.rfc2616_headers = 0
;;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads. ; /http://php.net/file-uploads file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not ; specified). ; /http://php.net/upload-tmp-dir upload_tmp_dir = "c:/wamp/tmp"
; Maximum allowed size for uploaded files. ; /http://php.net/upload-max-filesize upload_max_filesize = 2M
Also, my php.ini file is saved in: C:\wamp\bin\apache\Apache2.2.11\bin
I have had the same problem and the solution is quite simple. The default driver in PHP 5.3 for MS SQL Server does not work anymore in Windows. Microsoft has released a driver that you can use. The driver comes with documentation.
Here is the link where you can find the download: http://sqlsrvphp.codeplex.com/
Here is the link where you can find some documentation: http://msdn.microsoft.com/en-us/library/cc296172(SQL.90).aspx
Maybe php is using another ini file like C:\windows\php.ini, it has happen to me in the past :)
Well since you are using PHP 5.3 would it not make more sense to use the PDO Objects? Like so:
<?php
class DatabaseConn {
static function createNewMySQLConnection() {
try {
$conn = new PDO("mysql:dbname=" . MYSQL_DB_DATA . "; host=" . MYSQL_DB_HOST, MYSQL_DB_USER, MYSQL_DB_PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$x = new MyException("Failed to connect to db: " . $e->getMessage());
$x->errorMessage();
return null;
}
$conn->beginTransaction();
return $conn;
}
static function createNewPgSQLConnection() {
try {
$conn = new PDO("pgsql:dbname=" . PGSQL_DB_DATA . "; host=" . PGSQL_DB_HOST, PGSQL_DB_USER, PGSQL_DB_PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$x = new MyException("Failed to connect to db: " . $e->getMessage());
$x->errorMessage();
return null;
}
$conn->beginTransaction();
return $conn;
}
static function createNewMsSQLConnection() {
try {
$conn = new PDO("dblib:dbname=" . MSSQL_DB_DATA . "; host=" . MSSQL_DB_HOST, MSSQL_DB_USER, MSSQL_DB_PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$x = new MyException("Failed to connect to db: " . $e->getMessage());
$x->errorMessage();
return null;
}
$conn->beginTransaction();
return $conn;
}
static function runExecute($conn, $query) {
try {
$stmt = $conn->prepare($query->sql);
if (isset($query->input) && !is_null($query->input)) {
$stmt->execute($query->input);
return true;
} else {
$stmt->execute();
return true;
}
} catch (PDOException $e) {
if (isset($query->input)) {
$inputs = "";
foreach ($query->input as $i) {
$inputs .= "'" . $i . "' ";
}
$msg = "$query->sql with input(s) $inputs. statement failed, rolling back: " . $e->getMessage();
} else {
$msg = "$query->sql . statement failed, rolling back: " . $e->getMessage();
}
$x = new MyException($msg);
$x->errorMessage();
}
return false;
}
static function runPreparedQuery($conn, $query) {
$results = null;
try {
$stmt = $conn->prepare($query->sql);
if (isset($query->input) && !is_null($query->input)) {
$stmt->execute($query->input);
} else {
$stmt->execute();
}
$results = $stmt->fetchAll();
} catch (PDOException $e) {
if (isset($query->input)) {
$inputs = "";
foreach ($query->input as $i) {
$inputs .= "'" . $i . "' ";
}
$msg = "$query->sql with input(s) $inputs. statement failed, rolling back: " . $e->getMessage();
} else {
$msg = "$query->sql . statement failed, rolling back: " . $e->getMessage();
}
$x = new MyException($msg);
$x->errorMessage();
return null;
}
return $results;
}
static function getLastInsertId($conn) {
return $conn->lastInsertId();
}
static function closeConn($conn, $commit) {
if ($commit) {
$conn->commit();
} else {
$conn->rollBack();
}
$conn = null;
}
}
?>
It's not perfect but it works for me
精彩评论