php server could not find driver - php

I'm trying to set up the internal php server. When I run it I get the following message:
Fatal error: could not find driver in
E:\webProjects\skipper\drydockx\includes\functions.php on line 50
line 50 of the functions.php file: (at the bottom)
function query(/* $sql [, ... ] */)
{
// SQL statement
$sql = func_get_arg(0);
// parameters, if any
$parameters = array_slice(func_get_args(), 1);
// try to connect to database
static $handle;
if (!isset($handle))
{
try
{
// connect to database
$handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
// ensure that PDO::prepare returns false when passed invalid SQL
$handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch (Exception $e)
{
// trigger (big, orange) error
trigger_error($e->getMessage(), E_USER_ERROR); //LINE 50
exit;
}
}

You need to have a module called pdo_mysql. Looking for following in phpinfo()
pdo_mysql
PDO Driver for MySQL, client library version => 5.1.44
In Ubuntu/Debian you can use:
PHP5: sudo apt-get install php5-mysql
PHP7: sudo apt-get install php7.0-mysql
for Windows 8.1/10 in :\php.ini file you should uncomment line "extension=pdo_mysql"

Related

how to access sqlite3 database from cpanel using php

hi friends ,
class MyDB extends SQLite3
{
function __construct()
{
$this->open('/var/cpanel/eximstats_db.sqlite3');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
i have using eximstats db from server. while on updating my server the eximstats db get got under the SQLite3. i am new one to SQLite3 even though I have tried many more times access that db using the above php code but never i got result . please help me to improve this coding.
Is this code is correct . while running this i got "Fatal error: Uncaught exception 'Exception' with message 'Unable to open database: unable to open database file' "
thank you
You can simply use PHP PDO interface to access your SQLite3 database.
$db = new PDO('sqlite:/var/cpanel/eximstats_db.sqlite3')
PDO_SQLITE DSN
To access a database on disk, append the absolute path to the DSN prefix.
Just make sure that you have the PDO Driver for SQLite 3.x installed on your system.
Now to select a table just do:
$result = $db->query('SELECT * FROM tablename');
foreach( $result as $row ) {
print_r( $row );
}

Fatal error: can't find PHPUnit_Extensions_Database_PDB_DefaultDatabaseConnection in testcase.php

So I am trying to do some database testing in phpunit, and when trying to connect to the database, i get this error:
Fatal error: Class 'PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection' not found in D:\xampp\php\pear\PHPUnit\Extensions\Database\TestCase.php on line 145
I opened up testcase.php and checked line 145. This is line 143-146:
protected function createDefaultDBConnection(PDO $connection, $schema = '')
{
return new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($connection, $schema);
}
Also, heres the getConnection function i used, incase something is wrong with it:
public function getConnection() {
if ($this->conn === null) {
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$this->conn = $this->createDefaultDBConnection($pdo, 'test');
} catch (PDOException $e) {
echo $e->getMessage();
}
}
return $this->conn;
}
You need to install the Database extension. Issue the following commands in the console:
sudo pear config-set auto_discover 1
sudo pear install --alldeps pear.phpunit.de/DbUnit
(On Windows you need to omit the sudo and probably locate the path to pear.exe)

PDOException on Windows Server

I'm fairly new to MySQL and PHP, so bear with me. After some research, I found out...much to my distress...that apparently my site is hosted on a Windows server (by default) on GoDaddy which doesn't support PDO.
I just recently switched all database calls to use prepared statements through recommendation from another question I posted. I now find out that these aren't running and I'm getting a nasty error:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in D:\Hosting\8446415\html\ROOTFOLDER\php\Connect.php:8
Stack trace: #0 D:\Hosting\8446415\html\ROOTFOLDER\php\Connect.php(8): PDO->__construct('mysql:host=HOSTNAME', 'DBNAME', 'PASSWORD')
#1 D:\Hosting\8446415\html\ROOTFOLDER\admin.php(67): Connect->connect()
#2 {main} thrown in D:\Hosting\8446415\html\ROOTFOLDER\php\Connect.php on line 8
Here is Connect.php:
<?php
class Connect {
const expAddress = "HOSTNAME";
const expUser = "USERNAME";
const expPwd = "PASSWORD";
function connect() {
$db = new PDO('mysql:host='.self::expAddress.';dbname='.self::expUser.';charset=UTF-8', self::expUser, self::expPwd);
if(!$db) {
die("<p>Could not establish a connection to the database.</p>");
include('footer.php');
}
else {
return $db;
}
}
}
?>
So what is the fix here? I don't know what is supported, and I was told to shy away from all mysql_* statements.
I cannot upgrade my hosting account to Linux, even though that would be easiest. Do I need to use mysqli_* statements? How would this particular call change so that the input is safe from injection?
$stmt = $db->prepare("SELECT * FROM logins WHERE username=? AND password=?");
$stmt->execute(array($user, $pass));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
You need to make sure you have pdo_mysql installed. Check phpinfo() for pdo_mysql ... if you know it's installed, you may just need to uncomment the extension=php_pdo_mysql.dll in php.ini ...
To catch your create error and display it nicely, you can modify your code as such:
try{
$db = new PDO('mysql:host='.self::expAddress.';dbname='.self::expUser.';charset=UTF-8', self::expUser, self::expPwd);
} catch (PDOException $e) {
die('Connect Failed: ' . $e->getMessage());
}
return $db;
If you do have access to php.ini, make sure that this line is uncommented:
extension_dir = "ext"

Pear DB class not found

I have create a class called Database.php for interacting with MySql database using Pear Db class.
Database.php
<?php
require_once('DB.php');
require_once('cException.php');
class DataBase
{
private $dsn = 'mysql://root:xxxxxx#localhost/avatar';
private $conn;
//Constructor
function __construct()
{
global $conn;
$this->conn = DB::connect($dsn);
if(DB::isError($conn))
{
throw new DatabaseConnectionException();
}
}
//destructor
function __destruct()
{
$this->conn->disconnect();
}
public function select($query)
{
$conn->setFetchMode(DB_FETCHMODE_ASSOC);
$result = & $conn->query($query);
if(DB::isError($result))
{
return new SelectCommandException($result->getMessage());
}
return $result;
}
static public function instance()
{
static $objDB;
if(! isset($objDB))
{
$objDB = new DataBase();
}
return $objDB;
}
?>
And I am calling this class from a sample file test.php
test.php
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
require_once 'Database.php';
try
{
$db = DataBase::instance();
}
catch (DatabaseConnectionException $ex1)
{
echo $ex1->toString();
}
try
{
$sql = "Select * from register";
$result = $db->select($sql);
var_dump($result);
}
catch (SelectCommandException $ex2)
{
echo $ex2->toString();
}
?>
When I run test.php I get the following error
Warning:
require_once(/usr/share/pear/DB.php):
failed to open stream: No such file or
directory in
/var/www/Avatar/Database.php on line 2
Fatal error: require_once(): Failed
opening required
'/usr/share/pear/DB.php'
(include_path='.:/usr/share/php:/usr/share/pear')
in /var/www/Avatar/Database.php on
line 2
I don't know why I am getting this error. In phpinfo() it shows include_path .:/usr/share/php:/usr/share/pear .:/usr/share/php:/usr/share/pear
I am using php5 and I even tried installing php-pear package, still I get the same error.
I don't understand what's wrong here. Can someone please point me in a right direction.
Note : I have not installed php5 using sudo apt-get install php5. I have downloaded php5 packages using Keryx application.
Looks you did not install DB package, try in command prompt, do
pear list
If no package of DB is installed, you can installed it with
pear install DB
example from documentation

PEAR::DB Error, Extension Not Found

I am attempting to install phpBugTracker on our web server. When I attempt to test the database connection on the installation screen, I get an error screen that reads "DB Test Failure... DB Error: extension not found". The error is being thrown from the following function:
function test_database(&$params, $testonly = false) {
// PEAR::DB
define('PEAR_PATH', ''); // Set this to '/some/path/' to not use system-wide PEAR
// define('PEAR_PATH', 'inc/pear/'); // use a locally installed Pear (phpBT v0.9.1)
if (!#include_once(PEAR_PATH.'DB.php')) {
$error_message = translate("Failed loading Pear:DB");
$error_info = translate("Please check your Pear installation and the defined PEAR_PATH in install.php");
$error_info .= " <a href='http://pear.php.net/'>http://pear.php.net/</a>";
include('templates/default/install-dbfailure.html');
exit;
}
// execution gets this far without a problem...
$dsn = array(
'phptype' => $params['db_type'],
'hostspec' => $params['db_host'],
'database' => $params['db_database'],
'username' => $params['db_user'],
'password' => $params['db_pass']
);
$db = DB::Connect($dsn);
// Simple error checking on returned DB object to check connection to db
if (DB::isError($db)) {
// $db go boom...
$error_message = isset($db->message) ? $db->message : '';
$error_info = isset($db->user_info) ? $db->user_info : '';
include('templates/default/install-dbfailure.html');
exit;
} else {
if ($testonly) {
include('templates/default/install-dbsuccess.html');
exit;
} else {
return $db;
}
}
}
I am using MySQL version 5.0.45, PHP version 4.47, and I have PEAR::DB version 1.7.6 stable. I've already verified that I can connect to the database I'm using with the login I've created otherwise. I am at the mercy of my hosting company as to what modules are installed.
Any ideas on what could be causing the error?
Edit: db_type is set to "mysqli". When I use "mysql" as the type I get a "connection failed" error instead.
Okay, I feel rather silly, but the path to MySQL was different on this particular server and I had just assumed localhost. This had nothing to do mysql vs. mysqli. Fixed path and it connected just fine.
Verify with phpinfo() that extension for db_type you're using is installed and activated.
Perhaps you're trying with "mysqli" db_type, while you should use "mysql"
(without 'i')?
MySQLi doesn't come by default with PHP4.

Categories