Connect sqlite and php using netbeans - php

UPDATED
My latest code as follows:
$file ="sqlite:C:\New folder\\test.db";
echo $file;
$handle = new PDO($file) or die("Could not open database");
Error
sqlite:C:\New folder est.db
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php:20 Stack trace: #0 C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php(20): PDO->__construct('sqlite:C:\New f...') #1 {main} thrown in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php on line 20
I'm trying to use PDO driver however the error keep prompting me saying cant find the driver when it's in my extension php folder. I'm at my wits now.
I'm using SQLITE version 3 and PHP version 5.4.3.
I already placed the database file inside my r/w folder but it Fatal error: Call to undefined function sqlite_open(). I already researched on the web for alot of sources but the my directory path still returns some strange error (Sorry if i'm too noob, first time learning).
My code as follows:
$db ="C:\New folder\test.db";
Test to print my directory path of the database.
echo $db;
$handle = sqlite_open($db); //or die("Could not open database");
Debug result
C:\New folder est.db
Fatal error: Call to undefined function sqlite_open() in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php on line 18 which is "$handle = sqlite_open($db); //or die("Could not open database");"
Kindly advise where did I do wrong? The path I specified was correct but when I print out, it shows wrong path.
Thanks

one problem you have is that you are escaping (inadvertently) some of your path characters.
As your var_dump shows.
C:\New folder est.db
change your $db variable definition to
$db ="C:\New folder\\test.db";
or just put path in single quotes so PHP won't try and parse the string
$db ='C:\New folder\test.db';
and believe it or not in most cases unix paths will work:
$db ="C:/New folder/test.db";
as for the remainder of your problem, you need to make sure you have sqlite 2 enabled. View your php info and check to see if you a section like:
SQLite
SQLite support enabled
PECL Module version 2.0-dev $Id: sqlite.c 306939 2011-01-01 02:19:59Z felipe $
SQLite Library 2.8.17
SQLite Encoding iso8859
if not, in your php.ini make sure the line
extension=php_sqlite.dll
is uncommented and present (and the .dll exists in your extension folder).
However most people will recommend that you use the PDO driver for sqlite (and for any other database) as it will provide a more consistent paradigm across the different databases (you can use the same code with most databases).
[edit]
I found some comments in the php manual that may shed some light on your issues.
It seems that the directory that contains your sqlite database must be
writeable by the web server. Making just the file writeable won't
work.
and
Don't forget "extension=php_pdo_sqlite.dll" has to be enabled in
php.ini (if you use xampp is will be disabled by default) .

Related

PHP: Could not make sqlite3 database

I wanna create a sqlite3 database in PHP but I get an error from PHP
Error
Fatal error: Uncaught Exception: Unable to open database: unable to open database file
Codes
$database = new SQLite3('myDatabase.db');
Note: My PHP version is 7.1
Note: My database name is not duplicate and sqlite3 must make it
Thanks for attention
Your code is correct and should create the database file if it doesn't exist.
Most probably it's a user right issue, check the permissions of the folder where you are trying to create the file in.
Check the directory permissions. The folder containing the SQLite file must be writable by the appropriate user.

Could not find PDO driver (PhpStorm and SQLite)

I use PhpStorm to create a web page that saves data to a SQLite database. I have created tables in the DB, installed drivers in PhpStorm and it says, the connection is successful.
Ok, but when I try to open it in Chrome, it says:
Fatal error: Uncaught PDOException: could not find driver in C:\Users\maness\PhpstormProjects\icd0007\index.php:4 Stack trace: #0 C:\Users\maness\PhpstormProjects\icd0007\index.php(4): PDO->__construct('jdbc:sqlite:db1...') #1 {main} thrown in C:\Users\maness\PhpstormProjects\icd0007\index.php on line 4`
The code on line 4 within the index.php file is the following:
$connection = new PDO('jdbc:sqlite:db1.sqlite');
I have turned on every SQL extension in php.ini, tried different options - no result. Can you name the exact extensions needed to launch SQLite, or what am I doing wrong?
P.S.
PhpStorm SQLite Xerial drivers. I use PHP from XAMPP folder.
jdbc is not a valid PDO driver.
Since you want to connect to a SQLite database, remove jdbc from your dsn:
$connection = new PDO('sqlite:db1.sqlite');
You seem to have confused PhpStorm's SQLite connectivity with PHP's.

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

Cannot connect to MySQL Database with MDB2 (Error not Found)

In first point. I am absolutely a noob in PHP and PEAR. For exercising I have worked with PHP, Pear and Mondial DB of Oracle offline, but now I wanted to connect to my Database on 1and1.com.
Following I have tried:
$dsn = 'mysql://dbo5235xxxxx#10.24.xxx/db5235xxxxx'; //Have it tried with password too and many other variations
$sql = "SELECT * FROM Vereine";
$db = MDB2_Util::connect($dsn);
If I upload this file on my webserver and when I try to call this page, I get an error.
Fehler beim Verbindungsaufbau mit [mysql://dbo5235xxxxx#10.24.xxx/db5235xxxxx] : MDB2 Error: not found
The error Message:
"Fehler beim Verbindungsaubau" is an own deinied Message in MDB2_Util.
If I try to connect with MDB2::connect, then the Message calls only:
MDB2 Error: not found
What can be the Error? Why it doesnt show the real Error or a helpful hint. Can I debug? If yes, how?
Best Regards Benny
This can occur even if you have installed an MDB2 driver, but PHP can't find or can't read it.
As a practical example, I experienced this problem on a system on which the UMASK value had been changed from 022 to 027. Even though the MDB2 driver had been installed (with root privileges), the user under whom php-fpm was running lacked access to the library's files.

Windows 8.1 PHP PDO connection issues again

This is the common error often reported in questions:
SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Please assume correctly that I've been reading posts for ... 'days'!
I've done all the following:
1) Uncommented: extension=php_pdo_odbc.dll in php.ini
2) Set-up a system DSN in SysWOW64\odbcad32.exe
3) Checked that odbcjt32.dll is present in SysWOW64
My standard (latest PHP) code:
if (!file_exists($dbName)) {die("Could not find database file.");}
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=$dbq;Uid=$user;Pwd=$password");
}
catch (PDOException $e)
{
echo $e->getMessage();
}
There is no file_exists error (so addressing isn't the issue)
The .mdb is mine (so I know the correct Uid/Pwd)
The .mdb is in the same inetpub folder as the .php file calling it (so not permissions)
In windows powershell, I've run: Get-OdbcDsn which correctly shows the rich1 system DSN:
Name : rich1
DsnType : System
Platform : 32-bit
DriverName : Microsoft Access Driver (*.mdb)
Attribute : {DBQ, DriverId, ImplicitCommitSync, Threads...}
Still the same error :-( so I'm dead ended.
Does anyone know of any more 'Get-OdbcDsn' type routines that will help diagnose what's going on? Or any kind of new tack that leads me away from this dead end?

Categories