PHP: Dumping Memory sqLite Database to File - php

i'm trying to dump a sqLite Database from memory to file (for loggin purposes) but it gives me an error.
I've tried:
conn = new SQLite3("dump.db");
$db->backup( $conn );
but it gives me an error:
Call to undefined method SQLite3::backup()
I'm using sqLite Version 3.28.0
Any idea how it would work?

Your SQLite3 extension is old and doesn't have all the functions (SQLite3::backup has been introduced arround PHP 7.4).
See the manual page: https://www.php.net/manual/en/sqlite3.backup.php
As of SQLite 3.27.0 (2019-02-07), it is also possible to use the statement VACUUM INTO 'file.db'; to backup the database to a new file.
If upgrading is not an option, you can simply use copy().

Related

PDO mysqlnd; Undefined constant PDO::MYSQL_ATTR_READ_DEFAULT_GROUP

I have a LAMP stack, and I thought it might be nice to use the MySQL configuration file to allow PDO to connect to the MySQL database without the need to place DB credentials within our code structure
So I created a group in /etc/my.cnf.d/gggg.cnf
[clientGggg]
user=uuuu
password=pppp
database=dddd
and can now connect to the DB through the command line with mysql --defaults-group-suffix=gggg
I then tried to create a test DB connection in PHP with
$db = new PDO("mysql", null, null, [PDO::MYSQL_ATTR_READ_DEFAULT_GROUP => 'gggg']);
but I received the error
Undefined constant PDO::MYSQL_ATTR_READ_DEFAULT_GROUP
Checking the PDO docs for this attribute reveals the following:
PDO::MYSQL_ATTR_READ_DEFAULT_GROUP (int)
Read options from the named group from my.cnf or the file specified with MYSQL_READ_DEFAULT_FILE. This option is not available if mysqlnd is used, because mysqlnd does not read the mysql configuration files.
and checking my phpinfo(); reveals I am indeed using mysqlnd
Why is this limitation in place? Is there a workaround? Or am I doomed to write/load my credentials into PHP?

Phalcon Mongo Class not found error

I attempting to setup Phalcon PHP using a Mongo database connection. I have configured my bootstrap (index.php) file using the following:
// Mongo database connection
$di->set('mongo', function(){
$mongo = new Mongo();
return $mongo->selectDb("phalcon");
}, true);
// Collection Manager
$di->set('collectionManager', function(){
return new \Phalcon\Mvc\Collection\Manager();
});
Whenever I attempt execute an insert fucntion in using this connection, I receive a 500 Internal server error. Now, I have checked my apache server error logs and it states "PHP Fatal error: Class 'Mongo' not found in /var/www/phalcon-mongo/public/index.php on line 17".
I don't know why this request is not processing, according to the documentation given from Phalcon, the connection to mongo DB is setup up as I have displayed above.
If anyone has any advice, please let me know.
i think it's because your installation of Mongo is not valid.
try printing phpinfo() and check if mongo is loaded at all, if not - install it, add to ini files (if you use cli, don't forget to add to cli ini too) and reach the moment, when mongo is fully loaded.
try mongo w/o phalcon. any simple connection/insertation. you can see here: Fatal Error - 'Mongo' class not found that there are problems with apache module version for some people. Try reinstalling different mongo version.
if you can print this out:
echo Phalcon\Version::get();
there should be no problems with phalcon instalation
to validate mongo installation, try any of examples from php.net:
http://www.php.net/manual/en/mongo.tutorial.php
if both installations are valid, then there are problems with your custom code, but before doing anything you have to validate both installations.

Novice: Sqlite from php

I am completely new to php and wish to read data from a sqlite database.
I have got the hello world to work correctly but the following two scripts failed to execute without an error:
<?php echo sqlite_libversion(); ?>
<?php print_r(SQLite3::version()); ?>
The first script gives the error:
Fatal error: Call to undefined function sqlite_libversion()
The server on which I have my website has the following php configuration.
http://php.binerorockar.se/
where it shows:
pdo_sqlite: PDO Driver for SQLite 3.x enabled
Do I have sqlite3 support? How can I test this support?
There are several ways to access a SQLite from PHP. In your examples, you are using the SQLite and SQLite3 extensions for PHP. But you have neither of these extensions actually installed in your PHP system.
You do have the SQLite driver for PDO installed. This is the generic API for SQLite and other databases. The syntax would look like this to get the SQLite software version.
// connect to in-memory SQLite database
$db = new PDO("sqlite::memory");
echo $db->query("SELECT sqlite_version()")->fetchColumn();
To use the SQLite specific API functions, need to uncomment extension=sqlite.so in the PHP config file.

problems connecting sqlite database built using php5

I have used php5 to create a sqlite database. The database works fine when accessing its data using php.
However when I try and connect to the same sqlite file using firefox sqlite manager, I get the following error....
SQLiteManager: Error in opening file jTest.sqlite - either the file is
encrypted or corrupt Exception Name: NS_ERROR_FILE_CORRUPTED Exception
Message: Component returned failure code: 0x8052000b
(NS_ERROR_FILE_CORRUPTED) [mozIStorageService.openUnsharedDatabase]
My question:
Can you use php to create sqlite databases and have them work in other applications?
regards J

Issue with opening sqlite database file

I am trying to make use of sqlite for the first time, now I have used this :
$this->linkIdentifier = new SQLiteDatabase($database);
and it created a database just fine as website.sqlite opening it again doesn't give any problem but as soon as I use http://code.google.com/p/phpliteadmin/ to create a table and I try to open it again it gives me this error : file is encrypted or is not a database
What could be causing this ?
Just for clarification: There are SQLite database-files of version 2 and version 3. With "new SQLiteDatabase()", you create a db-file of version 2. With "new SQLite3()" you create a file of version 3.
PhpLiteAdmin has support for both versions if the appropriate php extension is installed. PhpLiteAdmin tells you the extension being used when you open the "structure" tab of the database under "SQLite extension". It should say "SQLiteDatabase" there if you open a version 2 database. If it does not, e.g. because this extension is not installed, you might not be able to edit a version 2 database in phpLiteAdmin.
The SQLiteDatabase extension might not be included in recent versions of PHP, e.g. in PHP 5.4, it is only available via PECL.
I suggest to use PHP's own SQLite3 functions to create and save a database. Make sure to set the SQLITE3_OPEN_CREATE flag.

Categories