My current Website Control Panel is PLESK Parallels, and i'm trying to use PDO MSSQL on my website.
I've searched the internet and i'm aware that the PDO dll needs to be in my php.ini file and such, but I keep reading different ways to do it all the time, using commands like YUM and i'm getting confused.
Can someone possibly, in plain, simple black and white instructions, explain the process so I can simply run code like this
try {
$hostname = "myhostname";
$port = myportnumber;
$dbname = "databasename";
$username = "user";
$pw = "password";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
When I load my page where this command is located i get the error message
'Failed to get DB handle: could not find driver'
Thanks
Example of installing pdo_dblib on CentOS-like OS for Plesk PHP 5.6:
# yum install plesk-php56-devel
# /opt/plesk/php/5.6/bin/pecl download pdo_dblib
# tar -xzf PDO_DBLIB-1.0.tgz
# cd PDO_DBLIB-1.0/
# /opt/plesk/php/5.6/bin/phpize
# ./configure --with-php-config=/opt/plesk/php/5.6/bin/php-config --with-pdo-dblib=./freetds
# vim pdo_dblib.c
On line #37 replace:
function_entry pdo_dblib_functions[] = {
with:
zend_function_entry pdo_dblib_functions[] = {
Save file and:
# make
# make install
you should see something like
Installing shared extensions: /opt/plesk/php/5.6/lib64/php/modules/
Enable extension:
# echo "extension=pdo_dblib.so" >> /opt/plesk/php/5.6/etc/php.d/pdo_dblib.ini
Verify:
# /opt/plesk/php/5.6/bin/php -m | grep pdo_dblib
pdo_dblib
Now sites in Plesk with PHP handler 5.6 will work with:
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
Pay attention for :
PHP DBlib PDO Issue
How to connect MSSQL from PHP 7, Plesk 12.5 installed on CentOS 7
PHP PDO works in OOPS .
Hence in PHP PDO first you must create a database handler using PDO class which is already defined in PHP Library. which will work as object for your database queries.
When you connect your database using PDO Class than you are ready to play with your queries with the database handler which you got during PDO connection.
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
Here $dbh will works as database handler object for MySQL Queries Operations.
I have a personal blog for PHP PDO here . Just go to reference and take a look how queries are working . Finally i would like to say it's very simple and secure.
Related
I have some problem. I have been looking it for almost a week. I have done include ibm_db2.dll, change php version but not working. When I run the db2 query, it shows Fatal error: Uncaught Error: Call to undefined function db2_connect() in C:\laragon\www\hpc_dev\db2_conn.php:15 Stack trace: #0 C:\laragon\www\hpc_dev\index.php(3): include() #1 {main} thrown in C:\laragon\www\hpc_dev\db2_conn.php on line 15
Currently I am using php version 7.4.12 64bit (ts) using laragon in Windows 10. I also have tested using Xampp and the same problem occur. Below are my codes to connect to ibm db2:
db2_conn.php:
<?php
ini_set("display_errors", 1);
$database = 'DB2';
$user = 'xxx';
$password = 'xxx';
$hostname = 'xx.xx.xx';
$port = 60000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
"HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
echo "Connection failed.";
}
?>
My code to show data from db2:
<?php
include("db2_conn.php");
$sql = "SELECT * FROM EMPLOYER WHERE EMPR_LOGIN_NAME = 'xxxxx'";
if ($conn)
{
$stmt = db2_exec($conn, $sql);
$row = db2_fetch_assoc($stmt);
echo $row['SECTOR_DESC'];
db2_close($conn);
}
?>
I don't know either the problem comes from db2 extension or others. Please help me.
For information, I have download the ibm db2 extension here: https://github.com/ibmdb/php_ibm_db2
Thanks
Your question omits details of the command-lines and actions you performed.
Most likely you missed a step in the configuration.
Remember to open the laragon terminal and verify that the Db2 driver is loaded:
php -m | grep ibm
should return ibm_db2 if you have correctly configured the correct version of the extension.
If you do not see that the ibm_db2 extension is loaded then any db2_connect will fail until you resolve the missing module.
For information the current laragon_full (November 2020) delivers PHP 7.2 TS, and that works correctly with the php_ibm_db2.dll TS for v7.2.x "out of the box" (which you must first download and configure).
Additionally, if I upgrade the PHP version for laragon to PHP 7.4.12 TS, and subsequently download and configure the matching php_ibm_db2.dll TS for php v7.4.x, then it all "just works" without any issues on Win 10 x64 2020 to connect to Db2-LUW and run queries etc.
It helps to verify that your Db2 client is correctly configured. You can do that independently of PHP. If you are using the clidriver Db2-client (the smallest footprint driver) you can configure the db2dsdriver.cfg XML file and then run db2cli validate -connect -dsn -user ... -passwd ... to verify that the connection works. If you are using either the Db2-runtime-client or the Db2 full client, or a local Db2-server, you can run (in the laragon terminal):
(after configuring your Db2 node directory and Db2 db directory via appropriate configuration commands )::
set DB2CLP=**$$**
db2 connect to $yourdsn user ... using ...
i use mamp pro on mac (catalina) and try to connect to remote mssql server.
in order to work with mssql i installed on the relevant php mamp folder:
brew install msodbcsql17 mssql-tools
pecl install sqlsrv pdo_sqlsrv
than i updated the php.ini file
extension=sqlsrv.so
extension=pdo_sqlsrv.so
when i try to run (of course with the correct credentails and server name)
$db = new PDO("sqlsrv:Server=MY.SERVER;Database=MYDBNAME", "MYUSER", "MYPASS");
i get this error:
Fatal error: Uncaught PDOException: SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712
what am i missing?
The PHP-PDO driver wants to access MS-SQL-server via the ODBC driver of SQL-server.
You need to install the odbc driver by doing
brew install msodbcsql17 mssql-tools
Afterwards, you need to enable TCP connections on sql-server, add a login, add the login to the correct role, and then you need to open port 1433, so TCP connections to SQL-server can work (unless you have both PHP and the sql-server run on the same machine).
And you might have to install UnixODBC:
brew install unixodbc
Also, you might have to add php_odbc to extensions
extension=php_odbc.dll
and php-fpm uses another ini file than php-cli/php-cgi.
On ubuntu, the PHP-fpm ini file is in
/etc/php/7.2/fpm/php.ini
while the other is in
/etc/php/7.2/cli/php.ini
Another alternative, if you can't get ODBC to work, is to use PDO_DBLIB, which uses FreeTDS instead of ODBC. That might be better anyway.
sudo pecl install pdo_dblib
However, that restricts you to php > 5.03 < 6.0.0.
Maybe you can compile it from source.
Mine works on Linux, all I had to do was:
sudo apt-get install php-fpm php-dev php-pear
sudo pecl install sqlsrv pdo_sqlsrv
(msodbcsql17 and mssql-tools I already had installed)
add the lines
extension=sqlsrv.so
extension=pdo_sqlsrv.so
into both ini files, and done.
Then I executed the connection-test-sample
php ./test.php
(i have port 2017, on docker), and it worked:
<?php
$serverName = "localhost,2017";
$connectionOptions = array(
"database" => "MY_DB_NAME",
"uid" => "sa",
"pwd" => "TOP_SECRET"
);
// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
die(formatErrors(sqlsrv_errors()));
}
// Select Query
$tsql = "SELECT ##Version AS SQL_VERSION";
// Executes the query
$stmt = sqlsrv_query($conn, $tsql);
// Error handling
if ($stmt === false) {
die(formatErrors(sqlsrv_errors()));
}
?>
<h1> Results : </h1>
<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $row['SQL_VERSION'] . PHP_EOL;
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
function formatErrors($errors)
{
// Display errors
echo "Error information: <br/>";
foreach ($errors as $error) {
echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
echo "Code: ". $error['code'] . "<br/>";
echo "Message: ". $error['message'] . "<br/>";
}
}
?>
<h1>Results: </h1>
Microsoft SQL Server 2017 (RTM-CU16) (KB4508218) - 14.0.3223.3 (X64)
Jul 22 2019 17:43:08 Copyright (c) Microsoft Corporation
Developer Edition (64-bit) on Linux (Ubuntu 16.04.6 LTS)
I get a funny warning, though:
PHP Warning: PHP Startup: Unable to load dynamic library
'/usr/lib64/php/modules/pdo_sqlsrv.so' -
/usr/lib64/php/modules/pdo_sqlsrv.so: undefined symbol:
php_pdo_register_driver in Unknown on line 0
If I remove
extension=pdo_sqlsrv.so
then it works, without warning.
Note:
Just fixed the crap.
If you run php --ini, it will list you all the ini-files php loads.
The correct location (on Linux) to put these two lines is
/etc/php/7.2/mods-available/pdo.ini
The above warning occurs if extension=pdo_sqlsrv.so is loaded before extension=pdo.so. Also, that way, you only need to set the extension ONCE, and it's done for both php-cli and php-fpm. The mods-available are then (already) symlinked into php-fpm and php-cli.
You might have the same problem on Mac.
To make sure there are no permission issues, create a test user that is sysadmin:
-- The available default languages:
-- SELECT * FROM sys.syslanguages AS sysl
CREATE LOGIN [WebServicesTest] WITH PASSWORD = 'TOP_SECRET'
,CHECK_EXPIRATION = off
,CHECK_POLICY = off
,DEFAULT_LANGUAGE = us_english;
EXEC master..sp_addsrvrolemember [WebServicesTest], N'sysadmin';
To test if the connection is actually working with the user, you can use AzureDataStudio.
Beware:
If your system uses OpenSSL 1.1 (e.g. Ubuntu 19.04), you need to download the insiders-build.
TDS-based providers (freeTDS/ODBC) need TCP open, even if you work on localhost.
if anyone needs the answer
turned out need to install one more thing:
brew install msodbcsql#13.1.9.2 mssql-tools#14.0.6.0
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) :)
There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.
I'm receiving the following error when trying to connect to my database via PDO:
Connection error: could not find driver
1. I've ensured that Apache is linked to my homebrew PHP.
$ which php
/usr/local/bin/php
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
The pdo_pgsql module shows up in php_info
3. My database connection is:
<?php
class Database
{
public $conn;
public function getConnection()
{
try {
$this->conn = new PDO("postgres://$user:$pass#$host:$port/$db_name");
print_r($this->conn);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
exit();
}
return $this->conn;
}
}
I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard
4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:
#listen_addresses = '*'
I'm still not having any luck and am looking at some guidance in this head scratcher.
As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
Install it if it is not
# yum install php-pgsql
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
Install PostgreSQL (but I think you already have this installed and
configured)
# yum install postgresql-server postgresql-contrib
Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
After
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Restart postgresql service
# service postgresql restart
Install PHP and PDO connector
# yum install php php-pgsql
Here is an example of PHP script I used to test connection:
<?php
// Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";
$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the $dbname database successfully!";
echo "\n";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
And the output:
# php pdo_test.php
Connected to the masterdb database successfully!
I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel