简体   繁体   中英

Connect sqlite and php using netbeans

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM