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 );
}
Related
Fatal error: Uncaught Error: Call to undefined method MongoDB\Driver\Manager::listDatabases()
this error keep showing when i wanted to use the administration command for mongodb using php. I do not know whats the problem here and someone with kind soul please help me. The following codes is what i have tried which cause that error.
<?php
$client = new MongoDB\Driver\Manager( 'mongodb+srv://'.$_ENV['MDB_USER'].':'.$_ENV['MDB_PASS'].'#'.$_ENV['ATLAS_CLUSTER_SRV'].'/test'
);
try{
$dbs = $client->listDatabases();
echo '<pre>';
print_r($dbs);
echo '</pre>';
// Or Nothing if you just wanna check for errors
}
catch(Exception $e){
echo "Unable to connect to Database at the moment ! ";
exit();
}
$colecciones = $client->listCollections();
foreach ($colecciones as $col) {
echo $col->getName();
}
?>
these two are the refereces that i used but is not working for me
Get collections in mongodb with PHP
Is there a way to test MongoDB connection in PHP?
what i am trying to do here is to make sure that my database connection is successful and also list out the collection name of my mongodb database.`
You are trying to call an undefined method in the MongoDB\Driver\Manager class
You can see the list of methods and functions here
https://www.php.net/manual/en/class.mongodb-driver-manager.php
In addition please follow all the listed function and methods in php mongodb driver.
Try to use this to query data from the database:
$manager = new MongoDB\Driver\Manager('mongodb+srv://'.$_ENV['MDB_USER'].':'.$_ENV['MDB_PASS'].'#'.$_ENV['ATLAS_CLUSTER_SRV'].'/test'
);
$filter = [];
$option = [];
$read = new MongoDB\Driver\Query($filter, $option);
$query = $mongo->executeQuery(‘db.Collection_Name’ $read);
$exportQuery = $query->toArray();
var_dump($exportQuery);
I am trying to get results from an API call using Slim connecting with MSSQLSERVER2012
the example used to work with MYSQL getconnection function (see below) but when I am trying to
connect with MsSQL server 2012 I am getting an error like "api call error "invalid data source name"
http://localhost/msapi/api.php/clients
1'api call error "invalid data source name"
require '/Slim/Slim.php';
$app = new Slim();
$app->get('/clients', 'getClients');
$app->run();
function getClients() {
$sql = "select * FROM clients";
try {
$db = getConnection();
$stmt = $db->query($sql);
$clients = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"client": ' . json_encode($clients) . '}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getConnection_MYSQL() {
$dbhost="SERVER";
$dbuser="USER";
$dbpass="PASSWORD";
$dbname="DB";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
function getConnection() {
$dbhost="SERVER";
$dbuser="USER";
$dbpass="PASSWORD";
$dbname="DB";
$dbh = new PDO ("ADODB.Connection");
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$dbhost.";UID=".$dbuser.";PWD=".$dbpass.";DATABASE=".$dbname;
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->open($connStr); //Open the connection to the database
return $dbh;
}
the function getConnection_MYSQL() is an example that works.
But the getConnection() tryis to connect with MS SQL server ?
Do you see why I am getting an "invalid data source name" with the function getConnection()?
Since you didn't post an alternative DSN-string, I'm assuming you are using the one from your example and just replace host, username and password. This won't work.
When you are running your Slim-application on a windows host you can (and should) use Microsoft's SQL Server Driver for PHP (sqlsrv).
There are 2 versions sqlsrv.dll and pdo_sqlsrv.dll. When you want to reuse most of your code you should use the latter. This way you probably only have to modify your DSN (see php docs):
new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
If you are using the first you have to update the way you connect to the db and create the query. You can read the Beginner's Guide to see a few examples that should make it easy.
If you are running not running your application on a Windows-machine you will probably have to set up ODBC and FreeTDS and then use PDO with an ODBC-DSN. From my experience this will be quite a lot of work, but there are a few good tutorials out there. Just google for "freetds sql server".
Iam so fresh with PHP so this problem so big to me. I dont know why and where my mistake is. Based on my research to connect to MSSQL database by using this code:
<?php
$run = mssql_connect('dev-svr05','sa','P#55w0rd', 'orlig_sm_dev');
if ($run)
{
echo "Connection OK";
}
else
{
echo "Connection Failed";
}
?>
But when i run this code i got this error message:
PHP Fatal error: Call to undefined function mssql_connect() in C:\Inetpub\wwwroot\phpscript\save_mssql.php on line 5
I using the same code to connect to MYSQL and its success but not with MSSQL. Can anybody please tell me why this is happen? Thank You
I would advice you download the sql server binaries from windows and use PDO.
try {
//In some occasions you only need to define IP/Hostname and you can forgo the \SQLEXPRESS part
$db = new PDO( "sqlsrv:Server=HOSTNAME\SQLEXPRESS;Database=DATABASENAME","USERNAME","PASSWORD");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// set this to your primary database
$db->query("USE DATABASENAME");
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server".$e );
}
Fourth parameter is expecting a true/false. Refer the below link and try:
http://php.net/manual/en/function.mssql-connect.php
Try "sqlsrv_connect" instead of "mssql_connect"
This question already has an answer here:
How to use PDO connection in other classes?
(1 answer)
Closed 2 years ago.
Hello i am new to PDO with MYSQL, here are my two files
1) index.php
require_once 'prd.php';
try{
$db = new PDO ('mysql:host=xxxx;dbname=xxx;charset=utf8', 'xxx', 'xxxx');
echo 'connectd';
}catch(PDOException $conError){
echo 'failed to connect DB' . $conError->getMessage ();
}
$conn = new prdinfo();
$conn->con($db);
2) product.php
class prdinfo{function con($db){
try{
foreach($db->query("select * from products where vendor_id = 2" ) as $row){
$prod_id = $row ['product_id'];
echo '<br/>' . $prod_id;
}
}catch(PDOException $ex){
echo 'an error occured' . $ex->getMessage();
}
}
}
my problem is here i can pass the connection object to every file, but i have so many files to use database queries, so i need to pass the $bd to all the files. this is getting burden on the code. so is there any way to connect the database with PDO.
Thanks
pdo.php, taken from here. People often overlook many important connection options, so I had to write a dedicated article that explains how to connect with PDO properly
product.php
<?php
class prdinfo
{
function __construct($db)
{
$this->db = $db;
}
function getVendor($vendor)
{
$sql = "select * from products where vendor_id = ?";
$stm = $this->db->prepare($sql);
$stm->execute(array($vendor));
return $stm->fetchAll();
}
}
index.php
<?php
require 'pdo.php';
require 'product.php';
$info = new prdinfo($pdo);
$vendor = $info->getVendor(2);
foreach ($vendor as $row)
{
echo $row['product_id'];
}
It would be also a good idea to implement class autoloading instead of manually calling require.
The simplest way of doing it is to do the database connectivity in a separate file like "database.php and then you can include this file on every new page you are creating...eg if you are creating a page like "dothis.php". then at the top of your dothis.php page write a statement include_once ('/path/to/your/file/database.php');
then you can use your $db object in the whole file wherever you want.
What you can do is to create a PHP file, let's say 'pdoconn.php'. In that file, prepare that $db object. Finally, for each of your PHP files, just include pdoconn.php at first. So, while your PHP files are being loaded, they will firstly connect to MySQL and give you the $db object.
Is there a way to connect a sqlserver using php-mssql DRIVER without a PDO connection on symfony?
I believe that you need to create your own driver for this purpose.
The abstract class Doctrine_Connection defines the way of connect.
On this class, in the method "connect" you can read:
if (extension_loaded('pdo')) {
find valid driver and load into PDO (calling PDO::getAvailableDrivers())
$found = true;
}
If driver is not found ( for example if you submit a dsn like this mssql_own:host=localhost;dbname=localdb ) then Doctrine_Connection makes:
$class = 'Doctrine_Adapter_' . ucwords("mssql_own");
if (class_exists($class)) {
$this->dbh = new $class($this->options['dsn'], $this->options['username'], $this->options['password'], $this->options);<br/><br/>
} else {
throw new Doctrine_Connection_Exception("Couldn't locate driver named " . "mssql_own");
}
You need to write this class named on my example "Doctrine_Adapter_Mssql_own" and make a connection with php-mssql.
I hope it helps...