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.";
}
Related
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
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);
?>
I'm using PHP 5.5 under Mac Yosemite, the default php with this SO. i'm trying to connect to MSSQL DB server but it's imposible with a lot of alternatives.
I tried to install freetds and the command works but when i tried with PHP...its look he is trying to load but the connection close. My code on PHP is like this:
$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";
$link = mssql_connect($server, $user, $pass) ;
if(!$link){
die('Something goes wrong');
}
I look into php info and it's enabled:
php info
¿Someone knows what is the best alternative to connect to mssql db and works?
Use mssql_get_last_message() to find out what the error is, then, fix the problem.
$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";
$link = mssql_connect($server, $user, $pass) ;
echo mssql_get_last_message();
echo mssql_min_error_severity();
die();
Right now it's working with these lines:
try {
$hostname = 'XXX.XXX.XXX.XXX';
$port = 1433;
$dbname = "YOUR_DB";
$username = "YOUR_USERNAME";
$pw = "YOUR_PASS";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
First you have to install PDO_DBLIB in your system.
I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
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