I'm developing an application with a php framework (Yii) who use pdo connection also for remote ODBC databases.
I'm working on a Debian server with PHP 7.1, with DBMaker drivers installed, ODBC.ini configured so DBMaker works well with this code:
if(!odbc_connect('MYDBDSN','myuser', 'mypass')) {
echo "Connection not found " . odbc_errormsg();
} else {
echo "Database exists";
}
// queries works!
But a connection made by
$conn = new PDO ("odbc:MYDBDSN", 'myuser', 'mypass');
says Unable to connect; PDOException: could not find driver.
Is there any way to connect my DBMaker database with PDO, making my queries framework compatible?
I needed to install and configure pdo_odbc drivers for PHP.
This link can be useful if your servers works under a Docker environment.
Related
I'm trying to connect to a MSSQL database with odbc in php.
My issue is similar to this issue, I can connect with tsql, but not with php.
The answer to the problem not work because I think I don't have SELinux installed (I don't know what it is, but pacman not found this package (or with a similar name) on my computer)
I don't understand why it doesn't work, odbc is installed and detected by php
print_r(PDO::getAvailableDrivers());
Array ( [0] => odbc )
I'm connecting by doing that:
$dsn = 'odbc:Driver=FreeTDS;Server=127.0.0.1;Port:1433;Database=[my base name]';
$pdo = new PDO($dsn, $user, $password);
My base is not in local, I use a ssh tunel because the base in accessible only at my school, and we need an ssh tunnel. And it work, I can connect myself to the base with tsql.
When I was connecting PHP to our MS SQL server, I could never get a connection established using ODBC drivers.
Instead, I downloaded the official PHP > SQL SERVER drivers direct from Microsoft. You can find them here.
Once installed, you must configure your php.ini file to include the new drivers, restart your web server and then use the following to open a new connection:
$conn = new PDO("sqlsrv:Server=SERVER_IP,1433;Database=DATABASE_TO_OPEN;");
I'm unable to connect to MSSQL database on smarterasp.net domain. I'm using:
PHP Tools for Visual Studio
Here is the code:
<?php
try {
$conn = new PDO("mssql:host=host_name_string;dbname=database_name_string", "username_string", "password_string");
// 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();
}
?>
and the error caught by the PDOexception class:
Connection failed: could not find driver
I've also tested my code online and again getting the same error(online tester url: http://sandbox.onlinephpfunctions.com).
I've searched for the solution and the only thing that could resolve my problem is by uncommenting:
extension=php_pdo_mysql.dll
but the line is already uncommented by default.
EDIT:
The following does not ressolve my problem (instead of mssql:host)
sqlsrv:host
dblib:host
Your problem is that you have not installed either the sql server client or the Microsoft pdo drivers on your machine. Please do that and make sure you can connect via a udp file.
Search SQL server client install and Microsoft pdo drivers
I have been trying to connect remotely to oracle using pdo
I have enable pdo driver
extension=php_pdo_odbc.dll
But getting this error
"SQLSTATE[HY000]: OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data (ext\pdo_oci\oci_driver.c:614)"
Here is my code for connection
try {
$dbh = new PDO("oci:dbname='My server ip'/orcl;charset=UTF-8", "my user name", "my password");
}
catch (PDOException $e)
{ echo $e->getMessage(); }
I had the same errormessage after I installed multiple copies of ODAC/ODT because I didn't know what version I needed to have my Visual Studio projects (Windows Forms app and a ASP.NET website) connect to a Oracle database. While fixing this connection problem in the end, I broke my PHP/PDO website which also needed to connect to the same Oracle database. It gave me the same errormessage as in your question.
I removed all ODAC/ODT client installations and also the Oracle Server to be sure and then I reinstalled only the Oracle Server (in my case Oracle 11.2 Express 64bit) and it worked again. I could connect again from SQL Developer and also through PHP/PDO. After that I installed the correct ODAC/ODT (32bit(!) version because of my 32bit Visual Studio) and now also my Visual Studio Forms project AND ASP.NET website could connect to the Oracle server.
I'm trying to connect to remote DB2 via PHP. But have some problems. I've already installed IBM Application developer client.
phpinfo() output:
IBM DB2, Cloudscape and Apache Derby support enabled
Module release 1.9.4
Module revision $Revision: 327944 $
Binary data mode (ibm_db2.binmode) DB2_BINARY
Then, I've got a php file which is looking like:
$database = 'MyDB';
$user = 'db2inst1';
$password = 'mypassword';
$hostname = '1.1.1.1';
$port = 50000;
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;PORT=$port;HOSTNAME=$hostname;".
"PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "connection to $database succeeded";
} else {
echo "connection to $database failed";
echo db2_conn_errormsg();
}
And trying to execute this file, I have "connection to MyDB failed", and NO visible response from db2_conn_errormsg(), which is actually making me baffled
Unfortunately, I haven't got a straight access to the remote server with database. But several months ago, when I was using other client, I succeeded to connect to exactly this database. But that time I didn't need to install IBM ADCL. That is why I can guess that problem is on this side. But even if so, I couldn't fix it.
Sorry if I duplicated some question on stackoverflow, but all answers, which I found, were unfortunately useless to me.
I'm using Apache 2.2 and PHP 5.4.
Hope you can help.
Thanks for any replies!
Are you sure you have connectivity to the server? Correct port, server, firewall rules, username, password, database name?
What is the SQL code you are receiving. Try to get the SQL code from PHP, "connection to xx failed" is your own code so it is useless to help you.
Did you install the application development client? which DB2 version are you using? ADCL is old, it was for DB2 8. Since DB2 9.7, clients have different names, and I think you need IBM Data server client in order to compile the php module. For more information, check this website: http://www-01.ibm.com/support/docview.wss?uid=swg27016878
I think you have to catalog the database server (node) and the database in the local machine with the db2 client. It seems that your PHP code uses ODBC driver, and it has to be configured locally.
Your connection string looks like an ODBC connection where as the db2_connect function in PHP needs :-
DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;
It's on the PHP web page.
I have never been able to get ibm_db2 or pdo_ibm working on a remote. They work if DB2 and Apache are on the same machine (like the iSeries) but not if the host is connecting to a remote DB2.
If you read the Doctrine2 PHP drivers for each you will find they redirect to ODBC if the host is not 'localhost' or '127.0.0.1'.
This code works for me
<?php
if (!$db = odbc_connect ( "AS400", $user, $password) )
echo 'error!';
$result = odbc_exec($db, "select count(*) from $table");
while (odbc_fetch_row($result)) {
var_dump($result);
print_r($result);
echo "\n";
echo odbc_result($result, 1)."\n";
}
odbc_close($db);
AS400 is DSN name defined in ODBC.
I'm trying to connect SQL Server 2005 using PHP. I searched in Google but am not getting proper solution, it's showing search results about mssql_connect() is not working properly. By seeing this am not getting anything.
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database) or die("Unable to connect to database: ".mysql_error());
This will connect mysql database. I tried replace mysql_connect() with mssql_connect(). But its not working. login.php has
$db_hostname = 'localhost';
$db_database = 'urlstore';
$db_username = 'root';
$db_password = 'tiger';
How can I connect SQL database using PHP.
You need to install MS SQL PHP extensions and then you can work with your MS SQL Server the way you are used to.
Here is the information about the extension and how to install it.
You could use PDO if your server configuration supports it. PDO is an abstraction layer that allows you to connect to many different database types using the same object.
<?php
try {
$dbh = new PDO ('mssql:host='.$mssql_server.';dbname='.$mssql_db, $mssql_login, $mssql_pwd);
$dbh->exec("INSERT INTO tablename(column1, column2) VALUES ('stuff', 'here')");
$dbh = null;
}
catch (PDOException $e)
{
echo $e->getMessage();
}
?>
This code can help you determine if PDO is enabled:
<?php
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
?>
OR you can run this:
<?php phpInfo(); ?>
and look for this:
PDO
PDO support PDO drivers
enabled dblib, mysql, odbc, pgsql, sqlite, sqlite2
If it isn't enabled for MSSQL, you can uncomment the "extension=php_pdo_mssql.dll" line in php.ini.
If it still doesn't work, you might want to try this:
ntwdblib.dll - The most common issue is that you do not have the
ntwdblib.dll file installed in your PHP directory (where php.exe is,
or sometimes placing it in the ext directory works as well). This
library can be found with your Enterprise Manager dll's or in your SQL
servers system32 folder. It's generally best to take the file from the
server where SQL Server is installed
-quoted from http://www.helpspot.com/helpdesk/index.php?pg=kb.page&id=13