PDO Exception error while connecting to microsoft azure - php

i have problem connecting to azure on my mac. I've read many articles here and added multiple extension to my php.ini file but nothing seems to work. I just want to simply run this code and connect to database. All the variables in the code have the actual values are correct.
At the moment it gives me following error : "PDOException Object ( [message:protected] => could not find driver".
i've looked throuh mutiple articles on this issue, and added extensions to php.ini. i've pasted all of the below for people to check. i also now that i have PDO attached to my server. Unforunately, i cant post the screemshot here, but my pdo_mysql, pdo_pgsql, pdo_sqlite in the phpinfo() call.
i would really appreciate any info and help on this matter. thank you!
$server = "tcp:*********.database.windows.net,1433";
$user = "jus***#********";
$pwd = "password";
$db = "testdb";
try
{
$conn = new PDO( "sqlsrv:Server= $server ; Database = $db ", $user, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die(print_r($e));
}
;Extensions
;extension=apcu.so
extension=imap.so
extension=yaz.so
extension=mcrypt.so
extension=gettext.so
extension=pgsql.so
extension=pdo_pgsql.so
extension=pdo_mysql.so
extension=php_pdo.dll
extension=php_pdo_mysql.dll
extension=php_pdo.dll
extension=php_pdo_firebird.dll
extension=php_pdo_informix.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_oci8.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=imagick.so

In addition to the extensions that you have enabled in php.ini you will need to install:
Microsoft Drivers 3.0 for PHP for SQL Server
You have to also make sure you are not using the wrong php.ini
Avoid these spaces in your connection string, and if you anything you should catch PDOException.
$server = "tcp:*********.database.windows.net,1433";
$user = "jus***#********";
$pwd = "password";
$db = "testdb";
$dsn = "sqlsrv:Server=$server;Database=$db";
try {
$conn = new PDO($dsn, $user, $pwd );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
die(print_r($e));
}

Related

PHP connection "Failed to get DB handle: could not find driver"

I'm using the following code to try and connect:
try {
$hostname = "*****";
$port = 1443;
$dbname = "*******";
$username = "********";
$pw = "************";
$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
This is my Error:
Failed to get DB handle: could not find driver
And the DLL Files are already in the php/ext and modified in the php.ini
What else can I do?
There is following of possibility here,
enable mssql in php.ini
;extension=php_mssql.dll
to
extension=php_mssql.dll
try editing your setting at the php.ini mssql.secure_connection must be set to on.
mssql_connect can use secured connection if php is configured ...
go into the php.ini and verify the mssql.secure_connection parameter
[MSSQL]
mssql.secure_connection = On
3.Try this:
Uncomment the following in php.ini by removing the ";"
;extension=php_pgsql.dll
I hope this will help you.

Connect PDO with Oracle database

I am new to Oracle, installed the Oracle today the 11g Express Edition.
Then I installed Java SDK, and then the free Oracle SQL Developer.
I connected with system account and created a username and table as defined below. I don't exactly know how Oracle works, I think instead of database name, usernames are used. So below are details.
Username/Connection/Database = CustomSearch
Table = Reservation_General_2
There are some columns inside that table and some data. but the point is I cant connect to Oracle Server.
Here is how I tried to connect to database server.
<?php
/**
* Created by PhpStorm.
* User: HaiderHassan
* Date: 9/3/14
* Time: 9:52 PM
*/
header('Access-Control-Allow-Origin: *');
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
";
try {
$conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
Problem is when I try to open that page, I get this error.
ERROR: could not find driver
These are my connection settings when I connect from Oracle Sql Developer.
What am I doing wrong, what steps should I take to fix this issue?
Update
I added the driver by removing semicolon from the php.ini file
extension=php_pdo_oci.dll
But I started getting this error.
The program can't start because OCI.dll is missing from your computer. Try reinstalling the program to fix this problem.
I have to click 4 time OK for different alert boxes that shows up. I also downloaded oci.dll and copied it to the windows/system32, but still getting this error. What to do?
Update
I uninstalled XAMPP and followed this guide to install Apache and PHP separately,
http://www.oracle.com/technetwork/articles/dsl/technote-php-instant-12c-2088811.html
and then I tried my luck. That driver Problem went away but there is new problem
ERROR: SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12521: TNS:listener does not currently know of instance requested in connect descriptor (ext\pdo_oci\oci_driver.c:635)
Here below is my new connection String.
try {
$conn = new PDO('oci:dbname=//localhost:1521/xe/ORCL', 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
I tried to follow this answer on Stack Overflow for making a connection string.
http://stackoverflow.com/questions/11970261/connect-oracle-with-pdo-with-sid-and-instance-name
Update 2
Also tried to check if drivers installed. I used this code
foreach(PDO::getAvailableDrivers() as $driver)
echo $driver, '\n';
Got this code from this below link
http://stackoverflow.com/questions/23239433/could-not-connect-to-oracle-using-pdo
it echoes this below line
oci\n
So this means that it is installed or this means some drivers are missing?
Update 3
Again rolled back to old connection just changed some stuff in that connection and seems like connection to oracle worked.
try {
$conn = new PDO("oci:dbname=".$tns, 'customsearch', 'babaji');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to database';
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
with this I get the message 'Connected to database', means echo works because there is no error given by PDO.
But problem is now my query is not working? What happened to my query? Or will I have to change the syntax of the query also as I connected to Oracle? Or is the connection still not working?
Check PDO and OCI drivers installed properly or not
Try with following code
class PDOConnection {
private $dbh;
function __construct() {
try {
$server = "127.0.0.1";
$db_username = "SYSTEM";
$db_password = "Oracle_1";
$service_name = "ORCL";
$sid = "ORCL";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";
//$this->dbh = new PDO("mysql:host=".$server.";dbname=".dbname, $db_username, $db_password);
$this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function select($sql) {
$sql_stmt = $this->dbh->prepare($sql);
$sql_stmt->execute();
$result = $sql_stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function insert($sql) {
$sql_stmt = $this->dbh->prepare($sql);
try {
$result = $sql_stmt->execute();
} catch (PDOException $e) {
trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
}
if ($result) {
return $sql_stmt->rowCount();
}
}
function __destruct() {
$this->dbh = NULL;
}
}
$dbh = new PDOConnection();
$dbh->select($select_sql);
$dbh->insert($insert_sql);
Have you installed the PDO driver? Look at the output of phpinfo() to see what's installed and/or enabled in your environment.
PDO Installation
If you're running PHP on linux, you can see what PDO drivers are available for your distribution by running yum list php-pdo. You can install the driver by running yum install php-pdo. You may also need to install a database specific driver for your database. Running a yum list php* will show you all the PHP extensions available for installation.
Database Specific Drivers
You need to install instant client on Windows, I used it and it work, see this video, the only that change is in the video when he execute install, you don't have to because in the new zip, doesn't have the execution file. I only have a problem when I make a SELECT query but the connection works just fine.
https://www.youtube.com/watch?v=cZDDI9HFBIU
Contact me if you have any question
I think your problem is with oracle listener configuration, your driver is ok, the error "listener does not currently know of inst.." means there is oracle configuration issue. You must ensure that the parameters in the listener file is exactly the same as in the connection string.
Also your connection string oci:dbname=//localhost:1521/xe/ORCL is incorrect, it should be oci:dbname=//localhost:1521/orcl (host:port/service_name) as indicated in listener.ora file. Ensure the correctness of your connection string using SQL developer.
you may check the below link, this link illustrates the matching of listener.ora and connection string parameters and there is php pdo code snippet at the end with the correct usage of the connection string.
https://www.youtube.com/watch?v=pMQXVihgrrE
https://adhoctuts.com/fix-oracle-io-error-the-network-adapter-could-not-establish-the-connection-error/
Wrong, Wrong & Wrong.
PHPinfo() will NOT enable the PDO driver nor will it show up.
You do NOT need to download a PDO driver separately the one packaged with your PHP installation will work fine.
You do NOT need to install the instant client as your PHP for windows will have the instant client built-in.
Solution:
Updating IIS7 with PHP manager or updating the PHP ini file within your installation to Enable the DLL.
extension=php_pdo_mysql.dll
extension=php_pdo_sqlite.dll
extension=php_pdo_sqlsrv.dll
[PHP_PDO_OCI]
extension=php_pdo_oci.dll

could not find driver php&pdo [duplicate]

i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.

PDO: Could not find driver php/mysql

i'm having issues connecting to my database on a web host that I have, i'm using the following:
$dsn = 'mysql:host=mysql1.hosting.digiweb.ie;dbname=mydbname';
$user = 'myusername';
$password = 'mypassword';
According to the website: Host Name mysql1.hosting.digiweb.ie (ip address)
as the title says i'm getting a could not find driver error, am i entering the host incorrectly i tried entering whats above and also the ip address - Thanks!
Edit:
Here's all my code
<?php
$dsn = 'mysql:host=localhost;dbname=';
$user = '';
$password = '';
try {
// Connect and create the PDO object
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo 'Database connection failed - ';
echo $e->getMessage();
exit;
}
echo 'works';
?>
go to your php.ini file and uncomment this line
extension=php_pdo_mysql.dll
and then restart your apache
Change your extension dir to be absolute in php.ini. I changed it from
extension_dir = "ext"
to
extension_dir = "C:/{PATH TO PHP DIRECTORY}/ext"
and it worked.

PDOException "could not find driver" on MAMP

I'm trying to work with PDO on my localhost. I'm running MAMP on OSX 10.7.4.
I've checked phpinfo(), and as far as I can see I should be fine.
I checked the php.ini to see that "extension=pdo_mysql.so" is in fact uncommented.
I read some were that I had to make the file PROJECTFOLDER/config/parameters.ini with the following content so i did, but with no luck. (Changed it to reflect my setup of cause)
database_driver = pdo_mysql
database_host = localhost
database_port =
database_name = databasename
database_user = msqlusername
database_password = mysqlpassword//if not make blank
mailer_transport = smtp
mailer_host = localhost
mailer_user =
mailer_password =
locale = en
secret = ThisTokenIsNotSoSecretChangeIt
Any ideas as to how I can get PDO up and running?
BTW I'm using the following code to make the connection:
try {
$host = 'localhost';
$dbname = 'ifjernsyn';
$user = 'root';
$pass = 'root';
# MS SQL Server and Sybase with PDO_DBLIB
$DBH = new PDO("mssql:host=$host;dbname=$dbname, $user, $pass");
$DBH = new PDO("sybase:host=$host;dbname=$dbname, $user, $pass");
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
# SQLite Database
$DBH = new PDO("sqlite:my/database/path/database.db");
}
catch(PDOException $e) {
echo $e->getMessage();
}
So there is not mssql and sybase 's driver.
You need PDO_DBLIB to access Microsoft SQL Server and Sybase databases.

Categories