The SQLite3 Class has an option like this.
$db = new SQLite3('mysqlitedb.db', SQLITE3_OPEN_READONLY);
In PDO you would simply open with:
$db = new PDO('sqlite:mysqlitedb.db');
My question is however, is there a way to open a database with PDO, in READONLY mode?
This will become possible with the release of PHP 7.3 (estimated for release in late 2018).
The syntax is as follows:
$db = new PDO('sqlite:mysqlitedb.db', null, null, [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY]);
Upstream commit
I don't think that's possible with pdo (yet?).
The pdo_sqlite driver of php 5.3 uses sqlite3_open() in pdo_sqlite_handle_factory() but you need sqlite3_open_v2() to pass the read only flag.
edit:
But a patch would be fairly easy. Take a look at pdo_mysql_handle_factory() in ext/pdo_mysql/mysql_driver.c and how it uses struct pdo_data_src_parser vars[] to parse the dns string.
Related
Can I somehow register php class as PDO driver?
To clarify my question. PDO driver works with dsn (for example mysql:host=localhost;dbname=test) where first part is which driver to use. In this example it is mysql and therefore it uses php extension pdo_mysql. Can I register new driver from php pointing on class?
Something like this:
PDO::registerDriver('foo', \Foo::class); // I know this does not exists and I'm asking if there is some way to do this
print_r(PDO::getAvailableDrivers());
// outputs: Array( [0] => mysql, [1] => foo )
I know I can extend and override php PDO and PDOStatement classes, but that is not what I'm asking.
I was looking into php source code but I'm not familiar with C/C++. Here is a link for function PDO::getAvailableDrivers if somebody is interested.
php is about to stop support for mysql functions, and there are lots of tools/recommendations for how to upgrade to upgrade to mysqli from mysql. I came up with this design, and I wonder if there are any issues that my solutions can have:
Create a file called translate.php and require_once it in all php files that use any mysql functions.
Create global variable in that file and call it $_CONN
overwrite default mysql function with the following (using overwrite_function which is not shown here)
$_CONN;
mysql_query($q)
{mysqli_query($_CONN,$q);}
mysql_error()
{mysqli_error($_CONN);}
mysql_connect($h,$u,$p)
{$_CONN=mysqli_connect($h,$u,$p);}
mysql_fetch_assoc($r)
{mysqli_fetch_assoc($r);}
mysql_fetch_array($r)
{mysqli_fetch_array($r);}
Are there any issues with my solution?
To move from mysql to mysqli in a painless way you have to move towards PDO
As I know PDO support has been added to codeigniter recently but I can't find any documentation or tutorial about how to actually use it. Can anyone tell how can I use it?
You can edit /application/config/database.php and to enable the PDO driver:
$db['default']['hostname'] = 'pgsql:localhost';
// or mysql:localhost
// or sqlite::memory:
$db['default']['dbdriver'] = 'pdo';
If you want to directly get you hand on the active DB connection. This might work, but I am not CI developer .. so no guarantees. I tried to understand that brain rotting code, but i suspect, that i failed. I'm not good at PHP4 + eval():
$CI = get_instance();
var_dump($CI->db->conn_id);
// should show that conn_id is instance of PDO
Well, since CodeIgniter is merely a PHP framework, nothing prevents you from using it natively, as in $pdo = new PDO(...);.
However, when they say PDO is now supported, I think they mean their normal Database class now uses PDO (rather than MySQLi or the such).
I'm using PDO to access two SQLite 3 databases in PHP. I want to switch the database files during a query by renaming them but I can't do that while the files are open as it gives an error that the file is being used by another process. I've tried turning off persistent connections and setting the handles to null but neither work.
Is there really no way to close a PDO handle and release the lock on the database file?
I believe unset($var) does that, I use it on my pdo sqlite project and it works like I want it to :)
Set all references to the handle to null (or to anything except the PDO object, really) and the runtime will destruct the object, which will close the connection.
$db = new PDO('...');
// Do some stuff
$db = null;
// Assuming this was the last reference to that PDO
// object, the runtime will destroy the object and
// its connection.
I have a project we are about to wrap up, but just got a request from the DBA to make all our connections SSL. So I switched the driver to mysqli in the object that turns out ADODB instances, but I don't see any native method to create secure connections.
To make things more difficult, there is a different set of certs and keys per connection type (read and write).
Anyone have any ideas?
The trick is to use a DSN in the
NewADOConnection() call (rather than
authenticating with a Connect() call)
and to use the mysqli driver. The DSN
syntax allows you to supply client
flags, and there's a mysqli flag for
using SSL certificates.
$dsn = 'mysqli://ssluser:sslpass#dbhost/test?clientflags=2048';
$dbh = NewADOConnection($dsn);
$sql = "show status like 'ssl_cipher'";
$res =& $dbh->Execute($sql);
print_r( $res->fields );
$res->Close();
$dbh->Close();
The answer to this question is found at:
http://mbrisby.blogspot.com/2008/06/adodb-php-mysql-ssl.html
Here is the reference to MySQL Client Flags:
http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol
I prefer the one with the Connect method, and clientFlags property.
$dbc = NewADOConnection('mysqli');
$dbc->clientFlags = CLIENT_SSL;
$this->dbc->Connect('dbhost', 'ssluser', 'sslpass', 'test');
Either of them would work.