How to establish a connection? - php

I am new to DB2.
$database = 'test';
$user = 'user1';
$password = 'pswd1';
$hostname = '10.250.10.10';
$port = 556;
$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 {
print($conn);
echo "Connection failed.";
die(db2_conn_errormsg());
}
all the values are correct. But connection have been failed. Plz advise me. and How to check in the PHPINFO(). whether the DB2 have been installed successfully or not.
I got the following error
Connection failed.[unixODBC][Driver Manager]Data source name not found, and no default driver specified SQLCODE=0

I'm not a PHP maven, but if you're using the ODBC driver, you probably need to catalogue the database in the ODBC driver manager. On Windows, search for ODBC in "Search Programs and Files". I'm not sure the equivalent on Linux

Related

PHP Warning: PHP Startup: Unable to load dynamic library 'php_ibm_db2.dll'

I want to install the db2 extension for PHP on Windows but it just isn't working. I've tried a lot of different solutions but I still get these:
php.exe
phpinfo()
When I try connection to db2 database
I'm using Apache on XAMPP on port 80. I have installed this: https://github.com/ibmdb/php_ibm_db2/tree/master/PHP%207.4.x/x64/TS
and placed it in my C:\xampp\php\ext folder
and set "extension=php_ibm_db2.dll" in my php.ini file. (which is in C:\xampp\php)
Only "db2" appearing in phpinfo()
Variable where db2 is appearing
PATH variable
So, I did not manage to connect to my db2 DB with the db2 extension, but here's an alternative I've found -->
$database = "xxx";
$hostname = "xxx";
$user = "xxx";
$password = "xxx";
$port = 50000;
# Build the connection string
$driver = "DRIVER={IBM DB2 ODBC DRIVER};";
$dsn = "DATABASE=$database; " .
"HOSTNAME=$hostname;" .
"PORT=$port; " .
"PROTOCOL=TCPIP; " .
"UID=$user;" .
"PWD=$password;";
$conn_string = $driver . $dsn;
# Connect
$conn = odbc_connect( $conn_string, "", "" );
if( $conn )
{
echo "Connection succeeded.";
odbc_close( $conn );
}
else
{
echo "Connection failed.";
}
output -->
Connection succeeded.
source -->
https://www.ibm.com/docs/en/db2woc?topic=programmatically-php

Problems with xampp

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

cant connect to mssql server or sqlsrv not showing up on phpinfo

I have problem to connect to SQL server. I did all the steps from here and I already changed the php.ini configuration file:
;On windows:
extension_dir = "D:\xampp\php\ext"
But I still can't connect, my PHP Version is 7.2.11. I tried with mssql_connect() and sqlsrv_connect():
This is my attempt:
<?php
$servername = "1111";
$username = "user";
$password = "123";
$dbname = "DEV";
$connection = mssql_connect($servername, $username, $password);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($dbname, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
} else{
echo "success";}
?>
MSSQL extension for PHP (mssql_ functions) and PHP Driver for SQL Server (sqlsrv_ functions) are two different extensions for PHP.
MSSQL extension is not available anymore on Windows with PHP 5.3 or later, so you need to install PHP Driver for SQL Server. You need to download appropriate version of this driver. For PHP 7.2 - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).
Also download and install an appropriate ODBC driver.
Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).
Example:
<?php
$server = '1111';
$database = 'DEV';
$uid = 'user';
$pwd = '123';
# SQL Server authentication
#$cinfo = array(
# "Database" => $database,
# "UID" => $uid,
# "PWD" => $pwd
#);
# Windows authentication
$cinfo = array(
"Database" => $database
);
$conn = sqlsrv_connect($server, $cinfo);
if ($conn === false) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
} else {
echo "success";
}
sqlsrv_close($conn);
?>

Can't establish connection to DB2

I am using Linux Suse 13.1. I have compiled the ibm_db2 extension for PHP successfully but when I run the connection test script it returns connection failed. I do not know where I am going wrong. Are there some other steps I need to do after installing the PHP extensions? Please note that I am using Data Client Server 9.7.
This is my script.
<?php
$database = 'Database_name';
$user = 'myusername';
$password = 'passwrd';
$hostname = 'xx.xx.xxx.xx';
$port = db_port;
$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.";
}

MSSQL using PDO to connect

I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.

Categories