Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I connect to an SQLite database with PHP? [duplicate]

Tags:

I have an SQLite database and am trying to connect to it with PHP. This is what I'm using:

<?php     $dbconn = sqlite_open('combadd.sqlite');      if ($dbconn) {         $result = sqlite_query($dbconn,  "SELECT * FROM combo_calcs WHERE options='easy'");         var_dump(sqlite_fetch_array($result, SQLITE_ASSOC));     } else {         print "Connection to database failed!\n";     } ?> 

However, I get this error:

Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in C:\xampp\htdocs\deepthi\combadd\combadd_db.php on line 4
Connection to database failed!

What's wrong and how can I fix it?

like image 859
user2412936 Avatar asked May 24 '13 05:05

user2412936


1 Answers

Try to use PDO instead of sqlite_open:

$dir = 'sqlite:/[YOUR-PATH]/combadd.sqlite'; $dbh  = new PDO($dir) or die("cannot open the database"); $query =  "SELECT * FROM combo_calcs WHERE options='easy'"; foreach ($dbh->query($query) as $row) {     echo $row[0]; } $dbh = null; //This is how you close a PDO connection 
like image 152
Antonio Carlos Ribeiro Avatar answered Sep 21 '22 11:09

Antonio Carlos Ribeiro