I am trying to connect PHP with SQL Server 2008 but I am unable to establish the connection.
I followed the following tutorials:
https://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx
My PHP.ini
My Dll and location:
My PHP Version:
My Extensions are showing in WAMP PHP Extensions:
But when I try the following PHP code:
<?php
$serverName = "SAPSRV"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"SmartLogistic");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
I am still getting the following Error:
( ! ) Fatal error: Call to undefined function sqlsrv_connect() in
H:\wamp\www\job\sql.php
Help me in Solving the issue, Thanks in Advance..
I had the exact same error a few months back and here is what worked for me.
First of all for the sqlsrv driver to work you will need 32bit WAMP Server it does not work with 64bit WAMP.
I used the php_sqlsrv_55_ts DLL file which I am able to run my queries within PHP on WAMP.
Now here is where it gets interesting. I am assuming you are going to be running your system on a Linux based server?
If it is the case sqlsrv does not work on Linux based OS so you will have to use the mssql driver on Linux OS. Here is a link to set it up.
Finally you will need to code a custom function which checks your PHP extensions and then depending where you are running them from choose the correct driver.
Here is a function I created to see which drive is loaded and determine which one to use.
// Function to check which MS SQL database driver is loaded
function get_db_loaded_extension() {
// Assign php loaded extensions to an array variable
$php_loaded_extensions_array = get_loaded_extensions();
// Loop through each php extension in the array
foreach($php_loaded_extensions_array as $php_ext) {
// Switch to check which MS SQL database driver is loaded
switch($php_ext) {
case "mssql":
$return = "mssql";
break;
case "sqlsrv":
$return = "sqlsrv";
break;
}
}
// Check if a MS SQL database driver have been found
if(!isset($return)) {
$return = js_dialog("No Microsoft SQL database driver loaded.");
}
return $return;
}
Just as an example also here is how you can create the connection.
// Call function to check which MS SQL database driver extension is loaded
$mssql_db_driver = get_db_loaded_extension();
// Switch to determine how to make the appropriate connection
switch($mssql_db_driver) {
case "mssql":
// Set the MSSQL database variables
$mssql_servername = "xxx.xxx.xxx.xxx";
$mssql_username = "my_user";
$mssql_password = "**********";
// Create conection to MSSQL using the mssql php extension
$mssql_conn = mssql_connect($mssql_servername, $mssql_username, $mssql_password);
break;
case "sqlsrv":
// Set the MSSQL database variables
$mssql_servername = "xxx.xxx.xxx.xxx";
$mssql_conn_info = array(
"UID" => "my_user",
"PWD"=> "**********"
);
// Create conection to MSSQL using the sqlsrv php extension
$mssql_conn = sqlsrv_connect($mssql_servername, $mssql_conn_info);
break;
default:
echo $mssql_db_driver;
break;
}
// Check the MSSQL connection
if(!$mssql_conn) {
exit("Could not connect to the database. Please contact the administrator.");
}
It looks like there is an issue with php rather than sql. sqlsrv_connect is an unknown function so it cannot be called. Try using PDO to start with. Its a nicer and safer way to interact with an SQL database.
http://php.net/manual/en/class.pdo.php
The following should allow you to query a sql server db.
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
$stmt = $dbh->prepare("SELECT * FROM Table");
$stmt->execute();
Related
I can not recognize the SQL Server command in XAMPP 1.8.1 and PHP 5.4.7, this code had already used it and it worked before but now it doesn't.
I have already tried to put the php_mssql.dll dll and still don't recognize the command:
$con = mssql_connect('187.164.1.2/base','pag','123') or die('Could not connect to the server!');
mssql_select_db('aguacom') or die('Could not select a database.');
I expected it to connect to the other server's database.
You are trying to connect to MS SQL Server using MSSQL PHP extension (mssql_ functions), but this extension is not available anymore on Windows with PHP 5.3 and removed in PHP 7.0.0.
What you can do is to install PHP Driver for SQL Server (sqlsrv_ functions). You need to download:
The appropriate version of this driver - for PHP 5.4 you need version 3.2 (32-bit or 64-bit depending on the PHP version)
The appropriate ODBC driver.
Note, that MSSQL PHP extension (php_mssql.dll) and PHP Driver for SQL Server (php_sqlsrv_54_ts.dll) are two different PHP extensions.
Example using mssql_ functions:
<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";
$conn = mssql_connect($server, $username, $password);
if ($conn === false) {
echo "Unable to connect. ".mssql_get_last_message()."</br>";
exit;
} else {
echo "Connected.</br>";
}
mssql_select_db($database, $conn);
// ...
mssql_close($conn);
?>
Example using sqlsrv_ functions:
<?php
$server = "187.164.1.2/base";
$username = "pag";
$password = "123";
$database = "aguacom";
$connectionInfo = array(
"UID" => $username,
"PWD" => $password,
"Database" => $database
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn === false) {
echo "Unable to connect. ".print_r(sqlsrv_errors(), true)."</br>";
exit;
} else {
echo "Connected.</br>";
}
// ...
sqlsrv_close($conn);
?>
you should enable mssql in xampp
things you need [download these files]: https://www.microsoft.com/en-us/download/details.aspx?id=20098
Add downloaded files in php.ini like this way
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
restart apache and then check
<?php
echo "<pre>";
print_r(PDO::getAvailableDrivers()); ?>
credits goes to rray
I have a SQL Server running the OS Windows Server 2012 R2. It is running Microsoft SQL Server 2012.
On another computer, I am running an IIS web server, and I have installed PHP version 7.2.13, and Microsoft Driver 5.3 for PHP for SQL Server.
I am trying to connect to a database on the SQL Server via PHP:
<?php
// Now connect to an Azure SQL database by setting Authentication to ActiveDirectoryPassword
$azureServer = "vgissql";
$azureDatabase = "dbo.pctProjectTracking";
$azureUsername = "myuid";
$azurePassword = "mypassword";
$connectionInfo = "Database = $azureDatabase; Authentication = ActiveDirectoryPassword;";
try
{
$conn = new PDO( "sqlsrv:server = $azureServer ; $connectionInfo", $azureUsername, $azurePassword );
echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
$conn = null;
}
catch( PDOException $e )
{
echo "Could not connect with Authentication=ActiveDirectoryPassword.\n";
print_r( $e->getMessage() );
echo "\n";
}
?>
If I try opening this php file in a browser, I get:
Could not connect with Authentication=ActiveDirectoryPassword. could not find driver
Any ideas on what could be wrong here?
I have a strange problem. My server has been running PHP 5.6 for years no problem. (WinServer Std 2K8 SP2, IIS 6, MySQL 5.6 {separate server}, PHP 5.6)
We connect it to a DB2 server at our parent company. Today (2017-02-14) the ODBC connection (PDO_ODBC) started returning "could not find driver".
Excel is able to use the same ODBC connection to query the database - the ODBC connection is working.
I tried using both the PDO method and procedural method to connect. Failures in seeing the driver both ways.
From phpinfo():
ODBC Data
PDO Data
Code snippet:
$dsn = "odbc:workingODBCdsn";
$user = "xxxx";
$password = "yyyy";
$conn = null;
$results = array();
try {
$conn = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
die($e->getMessage());
}
if ($conn) {
$qry = $conn->query($sql);
if ($qry) {
$qry->setFetchMode(PDO::FETCH_ASSOC);
foreach ($qry as $row) {
$results[] = $row;
}
}
}
print "<pre>" . print_r($results, true) . "</pre>";
//ALT Method
$conn = odbc_connect($dsn, $user, $password);
$results = odbc_exec($conn, $sql);
print "<pre>" . print_r($results, true) . "</pre>";
Thanks in advance for any help.
Are you using the unixODBC or ibm_db2 (http://php.net/manual/en/ref.pdo-odbc.php)? It it recommended to use IBM DB2 Universal Database with “ibm_db2” extension. It’s faster and more efficient than using generic driver. It calls the native IBM DB2 functions with the extension.
Check out the db2_* functions from php.net IBM DB2 functions manual
Server's PHP instance switched from IIS to IISExpress. Switching it back to (full) IIS, and enduring all appropriate PDO drivers were enabled fixed the problem.
i am trying to connect mssql database using sqlsrv_connect() php function but it's always show below fatal error.
Fatal error: Call to undefined function sqlsrv_connect().
Code:
$server = 'xx.xxx.xxx.Xxx\sqlexpress';
$username = 'uname';
$password = 'password';
$database = 'testdb';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $server, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
For this you have to download the associated driver.
The Microsoft SQL Server 2005 Driver for PHP allows PHP developers to access data in SQL Server 2005 and SQL Server 2008 databases. The driver includes support for Windows and SQL Server Authentication methods, transactions, parameter binding, streaming, metadata access, connection pooling, and error handling.
Check these links:
How to connect to MSSQL
Php Help Documnetation
I am new to Oracle, installed the Oracle today the 11g Express Edition.
Then I installed Java SDK, and then the free Oracle SQL Developer.
I connected with system account and created a username and table as defined below. I don't exactly know how Oracle works, I think instead of database name, usernames are used. So below are details.
Username/Connection/Database = CustomSearch
Table = Reservation_General_2
There are some columns inside that table and some data. but the point is I cant connect to Oracle Server.
Here is how I tried to connect to database server.
<?php
/**
* Created by PhpStorm.
* User: HaiderHassan
* Date: 9/3/14
* Time: 9:52 PM
*/
header('Access-Control-Allow-Origin: *');
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
";
try {
$conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
Problem is when I try to open that page, I get this error.
ERROR: could not find driver
These are my connection settings when I connect from Oracle Sql Developer.
What am I doing wrong, what steps should I take to fix this issue?
Update
I added the driver by removing semicolon from the php.ini file
extension=php_pdo_oci.dll
But I started getting this error.
The program can't start because OCI.dll is missing from your computer. Try reinstalling the program to fix this problem.
I have to click 4 time OK for different alert boxes that shows up. I also downloaded oci.dll and copied it to the windows/system32, but still getting this error. What to do?
Update
I uninstalled XAMPP and followed this guide to install Apache and PHP separately,
http://www.oracle.com/technetwork/articles/dsl/technote-php-instant-12c-2088811.html
and then I tried my luck. That driver Problem went away but there is new problem
ERROR: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12521: TNS:listener does not currently know of instance requested in connect descriptor (ext\pdo_oci\oci_driver.c:635)
Here below is my new connection String.
try {
$conn = new PDO('oci:dbname=//localhost:1521/xe/ORCL', 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
I tried to follow this answer on Stack Overflow for making a connection string.
http://stackoverflow.com/questions/11970261/connect-oracle-with-pdo-with-sid-and-instance-name
Update 2
Also tried to check if drivers installed. I used this code
foreach(PDO::getAvailableDrivers() as $driver)
echo $driver, '\n';
Got this code from this below link
http://stackoverflow.com/questions/23239433/could-not-connect-to-oracle-using-pdo
it echoes this below line
oci\n
So this means that it is installed or this means some drivers are missing?
Update 3
Again rolled back to old connection just changed some stuff in that connection and seems like connection to oracle worked.
try {
$conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to database';
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
with this I get the message 'Connected to database', means echo works because there is no error given by PDO.
But problem is now my query is not working? What happened to my query? Or will I have to change the syntax of the query also as I connected to Oracle? Or is the connection still not working?
Check PDO and OCI drivers installed properly or not
Try with following code
class PDOConnection {
private $dbh;
function __construct() {
try {
$server = "127.0.0.1";
$db_username = "SYSTEM";
$db_password = "Oracle_1";
$service_name = "ORCL";
$sid = "ORCL";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";
//$this->dbh = new PDO("mysql:host=".$server.";dbname=".dbname, $db_username, $db_password);
$this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function select($sql) {
$sql_stmt = $this->dbh->prepare($sql);
$sql_stmt->execute();
$result = $sql_stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function insert($sql) {
$sql_stmt = $this->dbh->prepare($sql);
try {
$result = $sql_stmt->execute();
} catch (PDOException $e) {
trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
}
if ($result) {
return $sql_stmt->rowCount();
}
}
function __destruct() {
$this->dbh = NULL;
}
}
$dbh = new PDOConnection();
$dbh->select($select_sql);
$dbh->insert($insert_sql);
Have you installed the PDO driver? Look at the output of phpinfo() to see what's installed and/or enabled in your environment.
PDO Installation
If you're running PHP on linux, you can see what PDO drivers are available for your distribution by running yum list php-pdo. You can install the driver by running yum install php-pdo. You may also need to install a database specific driver for your database. Running a yum list php* will show you all the PHP extensions available for installation.
Database Specific Drivers
You need to install instant client on Windows, I used it and it work, see this video, the only that change is in the video when he execute install, you don't have to because in the new zip, doesn't have the execution file. I only have a problem when I make a SELECT query but the connection works just fine.
https://www.youtube.com/watch?v=cZDDI9HFBIU
Contact me if you have any question
I think your problem is with oracle listener configuration, your driver is ok, the error "listener does not currently know of inst.." means there is oracle configuration issue. You must ensure that the parameters in the listener file is exactly the same as in the connection string.
Also your connection string oci:dbname=//localhost:1521/xe/ORCL is incorrect, it should be oci:dbname=//localhost:1521/orcl (host:port/service_name) as indicated in listener.ora file. Ensure the correctness of your connection string using SQL developer.
you may check the below link, this link illustrates the matching of listener.ora and connection string parameters and there is php pdo code snippet at the end with the correct usage of the connection string.
https://www.youtube.com/watch?v=pMQXVihgrrE
https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/
Wrong, Wrong & Wrong.
PHPinfo() will NOT enable the PDO driver nor will it show up.
You do NOT need to download a PDO driver separately the one packaged with your PHP installation will work fine.
You do NOT need to install the instant client as your PHP for windows will have the instant client built-in.
Solution:
Updating IIS7 with PHP manager or updating the PHP ini file within your installation to Enable the DLL.
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_sqlsrv.dll
[PHP_PDO_OCI]
extension=php_pdo_oci.dll