Best way to access sqlite database in read mode using php - php

Couple questions. Whats the best way to access a live .db file using sqlite to just read data?
Secondly
$url = "data.db";
try
{
$db = new PDO('sqlite:"$url"');
...
How can I correct this syntax so I can set the database url as a variable? If I must use a different method other than PDO because of question 1 please set it out with a variable for database url.
Thanks

Shamelessly taken from Opening SQLite3 as READONLY with PDO?:
Instead of PDO, try the SQLite3 library:
$db = new SQLite3($url, SQLITE3_OPEN_READONLY);
For more details, refer to PHP's doc:
http://php.net/manual/en/sqlite3.open.php

Related

2 mysql database connections

i have a question.:)
I have the following situation.
I have a script connection.php with make a mysql connection and fetch data from the database and write it on a variable $data.
After the script i use $db->close();
Now i want to use this $data in my second script connection2.php which make also a mysql connection to another database.
What is the best way to do this.
i Try to include the connection.php into my connection2.php.
But then i have a chaos with the mysql connections. Although i close each script with $db->close();
Can anyone help me how i can do this in the best way ?
with include it doesn´t work for me.
Thank you
This are different databases.
It depends on your code but you could create classes for the code in your two .php files and include one file in the other (or create one class and use two instances). Then do something like:
$conn1 = new ClassConnection1;
$data = response to some action;
//close $conn1
$conn2 = new ClassConnection2;//
//use $data with this new connection/database

How do I get database information after running pg_connect?

I have some code that runs a function and within it executes pg_connect.
$db = pg_connect("$dsn");
Is it possible to retrieve the database information from the $db variable, specifically the database name? If I run a var_dump on $db I get:
resource(18) of type (pgsql link)
Another approach is to use the PHP function that was specifically developed for this purpose:
$db_name = pg_dbname($db);
http://php.net/manual/en/function.pg-dbname.php
This saves you the SELECT.
You can simply run a command on the database you've connected to that asks for its name; as documented in the Postgres manual, the relevant query would be:
SELECT current_database() as database_name;
(I will assume you know how to run an SQL query using your $db variable, so won't bother with PHP samples.)

Detecting type of database connection in drupal

How can I detect what type of database I'm connecting to in drupal (using php code)? I am trying to write a module which exposes some database functionality which only works on postgres or sql server. Currently, I'm doing it by trying to detect the database version, since the syntax appears to be different for each database but it doesn't seem very robust. Is there a php function which will report this?
You should use the global variable: $databases and check the driver value.
global $databases;
dpm($databases);
https://drupal.stackexchange.com/questions/48882/how-to-get-database-credentials
I don't fully understand what you're trying at the moment but
db_driver()
returns he currently connected database in Drupal as a string (e.g. "pgsql").
This may be helpfull to you. Use db_set_active() API to set your database before executing the query.
This will help you to keep away from errors
<?php
//set your configurations
$db_url['default'] = 'mysql://drupal:drupal#localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd#localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd#localhost/yetanotherdb';
//set the active database and then process your queries in this case you can always knows which database is connected now.
db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
Setting multiple databases
<?php
// ... header of the settings.php file
$db_url = array (
"default" => "mysql://user:pass#host/db",
"second" => "pgsql://user:pass#host/db"
);
db_set_active('second');
Ref: https://drupal.org/node/18429

Why is PDO working without dbname?

I am connecting to my database using this command:
$resource = new PDO('odbc:driver=FreeTDS;Server=127.0.0.1;Port=8090;UID=Reporting;PWD=readonly;');
There is no dbname specified, and yet, it still connects to a database. The problem is, it is connecting to the wrong database. I tried including a section dbname=DATABASENAME;, but this was entirely ignored. How do I tell PDO to connect to a different database?
Use DATABASE instead of DBNAME, i think this is the problem:
$resource = new PDO('odbc:driver=FreeTDS;Server=127.0.0.1;Port=8090;DATABASE=DATABASENAME;UID=Reporting;PWD=readonly;');
did you try to do the Standard operation? like....
new PDO("odbc:Driver={SQL Server};Server=127.0.0.1;Database=test;",'sa','password');
and which database you are using? ms sql server or some else?

How do I detect database name using MySQL PDO

I am rather new to the PDO library, so I apologize for my inexperience. I am writing a class that uses the PDO library to build and execute queries and return the results, no matter what they are.
Within the class, I detect whether there is an open connection to a database, and if it is the same as the one being configured, it uses this one instead. This is really easy to do using the MsSQL library as the PDO::getAttribute() function returns 'CurrentDatabase' and 'SQLServerName', so I can just apply a condition like so:
if(!empty($this->PDO)){
// Get the current connection information
$current_connection = $this->PDO->getAttribute(PDO::ATTR_SERVER_INFO);
// Return if the connection is the same
if($this->connection_parameters['hostname']==$current_connection['SQLServerName']&&$this->connection_parameters['database']==$current_connection['CurrentDatabase']){
return;
}
}
However, when it comes to MySQL, the data returned from PDO::getAttribute is completely different and I cannot seem to get the database name from the current connection.
Does any body know a function or method to get the currently connected database of a MySQL connection using the PDO library in PHP?
I order to connect to both MySQL and MsSQL, you must have 2 connections. However, changing the database on a live connection is very simple.
The following simply checks if a PDO instance already exists and whether or not it is using the required database. If so then it continues with this connection, if not it changes the database.
// Test if the PDO object already exists
if(!empty($this->PDO)){
// If connection is the same then select the database
if($this->connection_engine==$this->PDO->getAttribute(PDO::ATTR_DRIVER_NAME)){
// Get the current database in use
$database = $this->PDO->query("SELECT {$this->select_db_function}");
$database = $database->fetchAll(PDO::FETCH_NUM);
$database = $database[0][0];
// If the current database matches the new database then return
if($database==$this->connection_parameters['database']){
return;
}
}
}
I see no point in looking for the opened connection and - especially - in checking for the current database.
Why can't you just open the connection, select the database for it and then use this connection all the time throughout your class - just like everyone does?
See comments on the MySQL manual page for 'USE database'

Categories