I am having a trouble to connect to MS SQL2000 with PHP through COM library.
The message I am getting is:
Could not establish a database connection exception 'com_exception'
with message 'Source: Microsoft OLE DB Provider for ODBC Drivers
Description: [Microsoft][ODBC SQL Server Driver][DBMSLPCN]SQL Server
does not exist or access denied.....
I have read through many posts and couldn't find any straight forward sample how to setup PHP in order to connect easily. These are the steps I've taken so far:
1) running MSSQL2000 with TCP/IP and Named Pipes enabled, listening on port 1433
2) installed PHP Version 5.4.16 TS
3) I have the following extensions enabled (related to mssql):
extension=php_com_dotnet.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_mssql.dll
extension=php_pdo_odbc.dll
4) I have checked for the COM windows permissions
5) I am sure I have provided the correct database/instance, username, password for the connection string.
6) when I instantiate the COM object with:
$connection = new COM("ADODB.Connection");
print_r($connection);
...I get the "com Object" output response, meaning the class seems to work fine.
What have I overlooked ? I have to connect through ADODB since the whole site I am working on is build on it. Is there a way to debug this? Any help is appreciated.
PROBLEM SOLVED!
After a few days of figuring this out and reinstalling virtually everything including Apache, different PHP and MSSQL versions I have come up with the following. I am not claiming this to be the best solution but it works:
Functional environment:
1) Windows Azure Server with MSSQL 2012
2) Apache 2.2 PHP 5.3.28 - installer: php-5.3.28-Win32-VC9-x86.msi downloaded from:
http://windows.php.net/download/
PHP Extensions (I am listing all but you need just the last few):
[PHP_BZ2]
extension=php_bz2.dll
[PHP_CURL]
extension=php_curl.dll
[PHP_ENCHANT]
extension=php_enchant.dll
[PHP_FILEINFO]
extension=php_fileinfo.dll
[PHP_GD2]
extension=php_gd2.dll
[PHP_GETTEXT]
extension=php_gettext.dll
[PHP_GMP]
extension=php_gmp.dll
[PHP_IMAP]
extension=php_imap.dll
[PHP_INTL]
extension=php_intl.dll
[PHP_LDAP]
extension=php_ldap.dll
[PHP_MBSTRING]
extension=php_mbstring.dll
[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll
[PHP_OCI8]
extension=php_oci8.dll
[PHP_OCI8_11G]
extension=php_oci8_11g.dll
[PHP_OPENSSL]
extension=php_openssl.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
[PHP_PDO_OCI]
extension=php_pdo_oci.dll
[PHP_PDO_ODBC]
extension=php_pdo_odbc.dll
[PHP_PDO_PGSQL]
extension=php_pdo_pgsql.dll
[PHP_PDO_SQLITE]
extension=php_pdo_sqlite.dll
[PHP_PGSQL]
extension=php_pgsql.dll
[PHP_SHMOP]
extension=php_shmop.dll
[PHP_SNMP]
extension=php_snmp.dll
[PHP_SOAP]
extension=php_soap.dll
[PHP_SOCKETS]
extension=php_sockets.dll
[PHP_SQLITE]
extension=php_sqlite.dll
[PHP_SQLITE3]
extension=php_sqlite3.dll
[PHP_SYBASE_CT]
extension=php_sybase_ct.dll
[PHP_TIDY]
extension=php_tidy.dll
[PHP_XMLRPC]
extension=php_xmlrpc.dll
[PHP_XSL]
extension=php_xsl.dll
[PHP_EXIF]
extension=php_exif.dll
php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll
extension=php_com_dotnet.dll
The php_pdo_sqlsrv_53_ts_vc9.dll extension pack was downloaded from: http://www.microsoft.com/en-us/download/details.aspx?id=20098
The extension=php_com_dotnet.dll downloaded from: http://originaldll.com/file/php_com_dotnet.dll/29343.html (cannot verify how secure this file is but it works)
The MSSQL 2012 needs to be set as follows:
when creating your database, open the management studio and right click the database instance in the left tree. Make sure that under the properties->security tab you select: "SQL Server and Windows Authentication Mode". Without this you will never connect if you are using the standard username / password method.
Then proceed with creating the database schema and under the main security tab create the new user with username/password related to this database. Make sure you set all permission right i.e: owner, datareader, datawriter.
The PHP connection script I used is as follows:
*===============================================*
function openConnection() {
$dbaseServer = "instance name";
$dbaseIp = "127.0.0.1";
$dbasePort = "1433";
$dbaseName = "databsse name";
$dbaseUser = "username";
$dbasePassword = "password";
$connectionString = ""
."Driver={SQL Server};"
."Network=DBMSSOCN;"
."Server=$dbaseServer;"
."Address=$dbaseIp:$dbasePort;"
."Database=$dbaseName;"
."Uid=$dbaseUser;"
."Pwd=$dbasePassword";
try {
$connection = new COM("ADODB.Connection");
$connection->ConnectionTimeout = 60;
$connection->Open($connectionString);
return $connection;
} catch (exception $e) {
echo "Could not connect - $e";
exit;
}
}
// test query
//===========
$sql = "SELECT * FROM [dbname].[dbo].[tablename]";
$connection = openConnection();
// unnecessary but in my case more time is needed.
$connection->CommandTimeout = 240;
$recordset = $connection->Execute($sql);
if (!$recordset) { $connection->Close(); die("Request failed!"); }
if (!$recordset->EOF) {
while (!$recordset->EOF) {
$test[] = $recordset[0]->value;
$recordset->MoveNext();
}
}
print_r($test);
$connection->Close();
--
I think that's it. Please feel free to correct my post and/or improve it :)
Related
I am trying to connect my SQL server with PHP using Xampp. I have already uploaded dll files in the ext folder but I am unable to connect it.
My PHP version is 7.2.6.
Uploaded dll files are - php_pdo_sqlsrv_72_ts.dll, php_sqlsrv_72_ts.dll.
I have written this code to connect my SQL database with PHP-
<?php
$serverName = "INDO-SERV\SQLEXPRESS,1443";
$uid = "sa";
$pwd = "XXXXXX";
$databaseName = "web";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>$databaseName);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);
?>
I am getting this error when I had tried this-
Fatal error: Uncaught Error: Call to undefined function
sqlsrv_connect() in C:\xampp\htdocs\biometric\db.php:7 Stack trace: #0
{main} thrown in C:\xampp\htdocs\biometric\db.php on line 7.
Anyone has an idea where I am doing wrong or how to connect with the database.
Installation of PHP Driver for SQL Server (sqlsrv and/or pdo_sqlsrv PHP extensions) can be done following the next steps:
Based on Microsoft PHP Drivers for SQL Server Support Matrix download appropriate version of this driver. In your case - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).
Download and install an appropriate ODBC driver - see System Requirements for the Microsoft Drivers for PHP for SQL Server
Load PHP Driver for SQL Server as PHP extension.
Restart Apache
Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).
my XAMPP version is 7.0.13
1- Download and Install "SQLSRV40.EXE" on this path:
D:\xampp\php\ext
2- Download and Install "msodbcsql.msi"
3- Edit file ":\xampp\php\php.ini" and add this extensions :
extension=php_sqlsrv_7_ts_x86.dll
extension=php_pdo_sqlsrv_7_ts_x86.dll
extension=php7ts.dll
4- restart xampp
Note: "SQLSRV40.EXE" Contain this extensions:
php_sqlsrv_7_ts_x86.dll , php_pdo_sqlsrv_7_ts_x86.dll , php7ts.dll
Download driver from:
https://download.microsoft.com/download/f/4/d/f4d95d48-74ae-4d72-a602-02145a5f29c8/SQLSRV510.ZIP
Unzip the files
Copy the dll files in C:\xampp\php\ext\
Open with your favourite editor the file php.ini located in C:\xampp\php\
Insert the extensions:
extension=pdo_sqlsrv_74_ts_x64
extension=sqlsrv_74_ts_x64
Restart Apache and PHP
For the new comers;
You can setup driver and integrate it as in this video explains so in a nutshell;
You should find drivers for php - sql server integration depending to your environment (versions) at links and download the driver that suits for your environment.
You should move the driver file (.dll for windows case) to php/ext folder.
You need to change php.ini as entering a new extension (for example extension=php_sqlsrv_7_ts.dll) by giving your exact file name you have moved to php/ext.
Restart your local server and you should see this extension in phpinfo(), if you can see it's there, you can connect to your db with your credentials and it's done.
Credits goes to creator of the video (applause) :)
I'm on Windows 10 and I'm using WAMP. I am trying to create a login form with prepared PDO statements. Below is the code to my connect.php script that is issuing the error: "Connection failed: could not find driver".
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'login';
try{
$conn = new PDO("mysqli:host=$server;dbname=$db", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected Successfully";
}catch(PDOException $e){
echo "Connection falied: " . $e->getMessage();
}
?>
This is a list from my php.ini file that shows and DLLs associated with pdo
extension=php_openssl.dll
extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_mysql.dll
extension=php_pdo.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
extension=php_pgsql.dll
;extension=php_shmop.dll
WAMP says "no dll file" for:
-php_mysqli
-php_pdo
I have the "php_pdo" dll file in wamp/bin/php5.6.25/ and in wamp/bin/php5.6.25/ext/ just in case.
I can't find a download for php_mysqli but I don't think I need that for PDO, though it would be nice to have if anyone has a download link.
So my question is, did I just install the dll file into the wrong directory, or what? I'm not sure what to do, But I just installed WAMP and I can't use PDO. Should I use another client(I'm not sure what they are called).
I think you have a typo, try mysql instead of msqli:
$conn = new PDO("mysql:host=$server;dbname=$db", $username, $password);
Also from comments:
Try to put this in the php.ini : extension_dir = "c:\php5\ext" and point to the extension
move php_pdo.dll before the other PDO extensions, and restart WAMP
Edit: I decided to change my answer to be a community wiki, since I feel there should not be any rep that should come of this. Plus, the question being a typo on more than one count.
Original answer:
The syntax is:
mysql:host
and not mysqli. I hope this isn't another "typo" as per your original post https://stackoverflow.com/revisions/40444027/1 of new PDO("msqli:host
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
As per the manual:
http://php.net/manual/en/pdo.connections.php
Note: Wamp should be ready to use "out of the box" and not require any drivers to be added.
I use Wamp also and xampp and never had a problem.
Plus, Wamp installation comes in different flavours (32 and 64 bit), so make sure you chose the right one.
http://www.wampserver.com/en/
You can also try installing xampp
https://www.apachefriends.org/
Use error reporting also, which would have been of help here:
http://php.net/manual/en/function.error-reporting.php
I am trying to connect to an Azure Microsoft SQL Server database on my php scripts. I cannot figure out why it isn't working. When I run my db_connection.php script, I get this error:
SQLSTATE[01002] Adaptive Server connection failed (severity 9)
When I run the tsql command, with the connection details for my azure ms sql database, the connection seems to work (I read the "1>" means the connection worked):
locale is "C"
locale charset is "ANSI_X3.4-1968"
using default charset "UTF-8"
Default database being set to iBalekaDB
1>
Inside my freetds.conf file, I have this configuration set up:
# server specific section
[global]
# TDS protocol version
tds version = 8.0
text size = 20971520
client charset = UTF-8
dump file = /tmp/freetds.log
debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
[iBalekaServer]
host = xxxxxxxx.xxxxxxx.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
My db_connection.php file looks like this:
try {
$dataSource = "dblib:host=iBalekaServer;dbname=iBalekaDB;";
$username = "xxxxxxxxxxxx";
$password = "xxxxxxxxxxxx";
$connectionObject = new PDO($dataSource, $username, $password);
$connectionObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($connectionObject) {
echo "<h2>Connection Successful</h2>";
} else {
echo "Connection Error";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
I ran tsql -C on the VPS and got this:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
I checked to see if I had pdo_dblib installed, and it was present when I ran phpinfo() on my Linux VPS Server.
What could be the issue here?
EDIT: using mssql_connect works. I really wanted to use PDO
On my test, I changed the $username to the format of UID (e.g. <username>#<db_server_name>), and it fixed your issue of SQLSTATE[01002] Adaptive Server connection failed (severity 9).
BTW, you can grab the UID from the connectionstring from Azure portal.
Additionally, if you get the issue of General SQL Server error: Check messages from the SQL Server (severity 16), you can refer to the answer of PDO DBLib not working.
Any update, please feel free to let me know.
I'm trying to create a Bluemix Application using PHP and ClearDB (using PDO to connect).
When I try to access the server using my localhost (WAMP) I can get the connection successful message. When I simply copy and past the code on Bluemix, I get the following error:
Fatal error: Class 'PDO' not found in /home/vcap/app/htdocs/includes/db.php on line 43
I'm sure that my credentials works because I used the same credentials to connect via MySQL Workbench.
I'm sure that my syntax is correct because I can connect using localhost (WAMP).
I searched and found that I need to activate the PDO as PHP Extensions (I already see this post Activating PHP extensions in Bluemix but no success).
Some configuration files:
.bp-config -> php -> php.ini
extension=pdo.so
extension=pdo_sqlite.so
extension=pdo_mysql.so
extension=sqlite.so
extension=mysqli.so
extension=mysql.so
extension=mbstring.so
extension=php_pdo.dll
extension=php_pdo_mysql.dll
display_errors = On
display_startup_errors = On
error_reporting = On
.bp-config -> options.json
{
"PHP_EXTENSIONS": ["bz2", "zlib", "openssl", "fpm", "tokenizer", "curl", "mcrypt", "mbstring", "PDO", "pdo_mysql", "mysql", "mysqli"]
}
line 43 from /home/vcap/app/htdocs/includes/db.php
$this->conn = new PDO($strcon, $this->user, $this->password)
Server Logs
Someone can help me to fix it?
Thanks!
I could solve it (with the help of #Jeff and # Marc), here is the solution:
The only thing I needed was:
.bp-config>options.json
{
"PHP_EXTENSIONS": ["bz2", "zlib", "openssl", "fpm", "tokenizer", "curl", "mcrypt", "mbstring", "pdo", "pdo_mysql", "mysql", "mysqli"]
}
I have been trying to figure out which dll and how to use it to connect to sql server.
It was much easier using the old php_mssql.
I am using Xampp on WinXP Pro SP3. I have been unable to figure out how to connect, i have search the manual, and none of the command's work.
I get PDO Error Driver Not Found
extension-php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll
I realized that I must use the SQLSERV 2.0 Drivers. But which dll is the correct one? And what syntax must I use to connect and run queries?
Thank you.
One way of doing this is using FreeTDS for Windows
I am assuming you have PHP >5.3
Download this http://download.moodle.org/download.php/dblib/php53/DBLIB_TS.zip
Add this line to your php.ini extension=php_dblib.dll
You will also need to make a file called freetds.conf in the root directory of your PHP installation.
It should look something like this:
[global]
host = xxx.xxx.xxx.xxx (host name or ip of the MSSQL server)
port = 1433
client charset = UTF-8
tds version = 8.0
text size = 20971520
Restart Apache and try running this script:
<?php
$link = mssql_connect('localhost', 'db_user', 'db_password');
if(!$link) {
echo'Could not connect';
die('Could not connect: ' . mssql_error());
}
echo'Successful connection';
mssql_close($link);
?>
hit me up on fb if this does not work ;)