2

I am using SQLite with PHP for the first time. I am trying to get data from contact.s3db. Bu I am getting an error like this: Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in... Here is my code:

$dir = 'contact.s3db';
$dbh  = new PDO($dir) or die("cannot open the database");
$query =  "SELECT * FROM person";
foreach ($dbh->query($query) as $row)
{
    echo $row[1] . " " . $row[2] . "<br />";
}
1
  • Try $dbh = new PDO('sqlite:/path/to/contact.s3db');. Commented Feb 22, 2016 at 9:43

2 Answers 2

6

You forgot the sqlite: prefix that tells PDO which driver to use;

$dir = 'sqlite:contact.s3db';
$dbh  = new PDO($dir) or die("cannot open the database");
Sign up to request clarification or add additional context in comments.

5 Comments

It gives Warning: Invalid argument supplied for foreach() in..
@HabipOĞUZ Sounds like it finds the provider now, but not the file. Try using an absolute path (a'la sqlite:/my/home/directory/contact.s3db)
But my database file and my php file is in the same directory and both of them are in the main directory (localhost). I am using WAMP and I activated all extension about SQLite. How can I understand is SQLite working properly?
@HabipOĞUZ The current directory is not necessarily the same as the directory the php file is located in, so if you want to be sure to find the file, you can either use an absolute path or use the macro dirname(__FILE__) which returns the directory the executing php file itself is in, and prepend that to the filename.
I solved the problem. While database creation, what I used "Without RowID". I don't know what is this option. But unfortunately I have used it. Thank you for your support.
1

You've to specify the driver, use $dbh = new PDO('sqlite:contact.s3db'); instead

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.