Php mysql connection with PDO on IIS - php

The following code on execution shows :
could not find driver.
I have already changed extension=php_pdo_mysql.dll from comment and also restarted the server but it still shows driver couldn't found.
I'm using mysql5.7 php 7.1.6 and iis10.
<?php
$hostname='localhost';
$username='root';
$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=stickercollections",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to Database<br/>';
$sql = "SELECT * FROM stickercollections";
foreach ($dbh->query($sql) as $row)
{
echo $row["collection_brand"] ." - ". $row["collection_year"] ."<br/>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

Make sure you have enabled the extension in php.ini .
Also check PHP info and verify that the extension is indeed installed. This can be done by creating a file called info.php in the htdocs folder with the following content
<?php
phpinfo();
?>

Related

Debian 11 PHP db2_connect() not working / PECL installed ibm_db2

I have taken the following steps and I am using Debian 11.
I've installed the CLI Driver for DB/2 from the IBM Website
I've installed the ibm_db2 package via pecl for PHP PECL
I've edited the PHP.INI Files for both apache2 and CLI.
My PHP script is executing and it acceptes the db2_connect() command
Nevertheless, it's not connecting to my DB/2 server. I've ensured that I have connectivity from the Debian Server to the DB/2 Server.
I am getting constantly the following message:
PHP Warning: db2_connect(): SQLGetDiagRec returned -2 (is the driver working?) in /var/www/html/db2.php on line 7
Connection failed.Done Processing
Any help is appreciated.
Is the $database string correct? Are there any other setup setps to be taken?
This is my PHP script so far:
<?php
$database = 'NAMZPR1:50055';
$user = 'bouser';
$password = 'xxx';
$conn = db2_connect($database, $user, $password);
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
echo "Connection failed.";
}
echo "Done Processing ...";
?>
It has turned out that either the db2dsdriver.cfg has to be edited or a valid connection string in PHP has to be used. Here is a sample PHP.
<?php
$connection_string="DATABASE= ;HOSTNAME= ;PORT= ;PROTOCOL=TCPIP;UID= ;PWD= ";
$conn = db2_connect($connection_string, "", "");
if ($conn) {
echo "Connection succeeded.";
db2_close($conn);
}
else {
echo "Connection failed. " . db2_conn_errormsg() . " ???";
}
?>
Also, ensure that the environment variables have been set. Thanks and acknowledgement to "mao".

could not find driver in PDO_FIREBIRD using PHP

I am getting Error: Could not find Driver while connecting to Firebird DB using PHP. My driver setting in PHP.INI file are OK with enabled extensions. It is showing PDO_firebird clicked. I have also copied firebirdclient dll file in system32 folder. tried doing anything i can but still its showing the same error. My firebird db file is in the same location. port for locathost access is 85. Anyone's help will be really obliged. Code i am writting with the help of some suggestion is as under:
<?php
$server = 'localhost:c:/xpos_db/';
$username = 'sysdba';
$password = 'masterkey';
$db = 'xposdata.fdb';
try {
$dbh = new PDO("firebird:host=$server;dbname=$db", $username,
$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected Successfully";
} catch (PDOException $e) {
print "Problemo!: " . $e->getMessage() . "<br/>";
die();
}
?>

PHP PDO connection failing without error

It's been many years since I did any dev in PHP.
The concept of connecting to MySQL using PDO is completely new to me, and I don't seem to be able to get it to work.
Using MySQLi, and the following code, which I know is deprecated, works nicely:
<?php
include 'models/db_details.php';
$db_connection = mysql_pconnect("$dbhost", "$dbusername", "$dbpasswd") or die("Couldn't connect to server: " . mysql_error());
$db = mysql_select_db("$dbname", $db_connection) or die("Couldn't select database.");
?>
<h1>After connecting to the DB</h1>
As expected, this displays:
After connecting to the DB
From all of the pages I have read, using PDO, this should look something like:
<?php
include 'models/db_details.php';
try
{
$conn = new PD0("mysql:host=$dbhost;dbname=$dbname;charset=utf8", $dbusername, $dbpasswd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<h1>After connecting to the DB</h1>
The result of this is a completely blank page.
All of this is on a FreeBSD box, running:
Apache/2.4.25
PHP/5.6.30
MySQL 5.6.35
I have uncommented extension=php_pdo_mysql.dll in php.ini.
I can also confirm that the MySQL driver for PDO is installed:
What am I missing?
Check the font. Make sure it is pdo and not pd0 (zero).
Magic happens after that.

PDO connect to Oracle

I'm trying connect to Oracle.
This is my code:
$dsn="oci:dbname=//oracleserver:1521/xe;charset=AL32UTF8";
$user="portal2";
$password="portal2";
try{
$arrConnectOptions = array(PDO::ATTR_PERSISTENT => true);
$arrConnectOptions += array(PDO::ATTR_CASE => PDO::CASE_UPPER);
$arrConnectOptions += array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$arrConnectOptions += array(PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING);
$dbh = new PDO($dsn, $user, $password, $arrConnectOptions);
$sql = "select count(*) from tbl_message";
foreach ($dbh->query($sql) as $row) {
var_dump($row);
}
unset($dbh);
echo "OK";
}catch (PDOException $e){
print("Error:".$e->getMessage());
echo "NG";
header("HTTP/1.1 500 NG");
die();
}catch (Exception $ex){
print("Error: ".$ex->getMessage());
}
$dbh = null;
When I execute on Linux command line: php check.php, It's ok
But when I try to open on web browser. I got error message
"Unable to load the webpage because the server sent no data.
Error code: ERR_EMPTY_RESPONSE"
P/S: Server is running. (Centos 7)
Please help me
Do you have dispaly_errors set to 1 and error_reporting set to E_ALL? My best guess is that since it is common for PHP CLI and the web server to use different PHP.ini files, you have the OCI driver enabled in the CLI ini and not in the web server ini. If you web server ini isn't set to display the errors you could get the error above.

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

Categories