Novice: Sqlite from php - 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.

Related

PHP: Dumping Memory sqLite Database to File

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().

Error and warning connecting MongoDB to PHP using php_mongodb extension

I installed the MongoDB PHP driver on Windows 10 (I'm using WAMP equipped with PHP 5.6.25. following the istructions I found at http://php.net/manual/en/mongodb.installation.windows.php and I installed also the libbson and libmongoc libraries (requested as requirements) as written at http://php.net/manual/en/mongodb.requirements.php.
Then, I added the bin folders of MongoDB, libbson and libmongoc to system path.
However, even if I can see the php_mongodb extension in the extensions list of WAMP, launching phpinfo() the mongo extension doesn't appear with the others.
Furthermore, tryng to connect to my database with
<?php
$mongo=new MongoClient("");
$db=$mongo->galileo;
$collection= $db->items;
print_r("Number of documens: "); ?>
I got the error
Fatal error: Class 'MongoClient' not found in C:\wamp64\www\galileo\index.php >on line 21
At a first look, reading this error, it might seem like that PHP is looking for php_mongodb extension in the uncorrect folder i.e. C:\wamp64\www\galileo\index.php (where the index page of my project is placed) instead of the correct one C:\wamp64\bin\php\php5.6.25\ext where all the extensions are.
But, looking at php log file php_error.log I find also a warning that says:
PHP Warning: PHP Startup: Unable to load dynamic library 'c:/wamp64/bin/php/php5.6.25/ext/php_mongodb.dll' - Il sistema operativo non pu� eseguire %1.
in Unknown on line 0.
(for not Italian speaking, the phrase after - means the operating system can't execute %1, even if I can't imagine what %1 stands for).
Even using the new class MongoDB\Driver\Manager I get the error
Fatal error: Class 'MongoDB\Driver\Manager' not found in C:\wamp64\www\galileo\index.php on line 21
and the same warning.
Do you notice some error or forgetfulness in the installation process as I described and, if not, do you know how to fix the problem?
The problem is certainly related to WAMP and I think is related to the multiple php.ini in his folders. In fact, in the apache folder you can find a php.ini file that cannot be modified, otherwise nothing works at all; at the same time any changes made to the php.ini file in the php folder seems have no effect except making appear the mongodb extension in the extensions list.
So, I try using XAMPP, as suggested in this video tutorial and it works. Using Composer I was able to install also the PHP library and not only the driver.
you should not use 'MongoClient class' anymore, this extension that defines this class is deprecated. look at here.
instead, you should use MongoDB\Driver\Manager class. please read http://php.net/manual/en/class.mongodb-driver-manager.php.
and the setup must be like this in php:
$mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");
And if you still use the old class; you either need to install the old legacy extension (pecl install mongo) and use PHP 5.x, or update your code to use this new driver's classes as the old driver is not available for PHP 7. There is an upgrade guide at here.
the last part is from derickr's answer in this issue on github: https://github.com/mongodb/mongo-php-driver/issues/300

Cant make PDO_sqlite working php

I am developing a website. I need to use PDO. I ran phpinfo(); and it said:
PDO Driver for SQLite 3.x enabled
SQLite Library 3.8.10.2
Although writing
$pdo = new PDO("path/to/db.db");
Gives me error if as this function doesn't exist.
Uncaught exception 'PDOException' with message 'could not find driver`.
Am I doing something wrong? (also that db file is sqlite 3 format). Any advice? Thanks ! (btw my website is made with wordpress)
It should be $pdo = new PDO("sqlite:path/to/db.sqlite");
2nd result from Google http://henryranch.net/software/ease-into-sqlite-3-with-php-and-pdo/

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.

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

Categories