1

I have Apache web server installed on Manjaro(Sway) linux, and default location of projects is: /srv/http/

I have created a folder for my project by the name test in the location and have created a database named db1.db inside it through terminal using the following steps (followed this article):

sqlite3 db1

sqlite> create table tblone(one varchar(10), two smallint);

sqlite> insert into tblone values('helloworld',20);
sqlite> insert into tblone values('linux', 30);

I can verify that the table has been created successfully by running the following command:

sqlite> select * from tblone;

which outputs:

helloworld|20
linux|30

I need to create a PHP file which connects the above created database using PDO.

So, inside test folder, I create a file index.php with the following content:

<?php
    $myPDO = new PDO('sqlite:test/db1.db');
    $result = $myPDO->query("SELECT * FROM tblone");
    foreach($result as $row){
        echo $row['one']. "\n";
    }
?>

But I get a blank white screen in output, when I enter http://localhost/test/index.php in browser. Where am I going wrong?

7
  • Check your web server log files, you're likely getting a fatal error. The path to the db file is relative to the PHP file, so you probably just want 'sqlite:db1.db'. Commented Oct 8, 2024 at 19:24
  • @AlexHowansky, thanks for the comment. I tried setting the path to db1.db only, but that didn't help. The web server log file states this: [Wed Oct 09 01:11:57.508603 2024] [php:error] [pid 4497:tid 4497] [client ::1:42034] PHP Fatal error: Uncaught PDOException: could not find driver in /srv/http/test/index.php:2\nStack trace:\n#0 /srv/http/test/index.php(2): PDO->__construct()\n#1 {main}\n thrown in /srv/http/test/index.php on line 2 . But I am not able to understand it? What should I do? Can you help? Commented Oct 8, 2024 at 19:47
  • "could not find driver" means that your PHP installation does not include the required module to support SQLite. If you do php -m from the command line, you'll get a list of installed modules, which should include pdo_sqlite but probably doesn't. Not familiar at all with Manjaro/Sway so can't help in regards to how you'd install that. It might be as simple as uncommenting (or adding) the line extension=pdo_sqlite.so in your php.ini file. Commented Oct 8, 2024 at 19:54
  • @AlexHowansky, just checked and I see that I haved pdo_sqlite installed, Have uncommented the pdo_sqlite in the php.ini file as well. Now I am getting the following message in the log: Uncaught PDOException: SQLSTATE[HY000] [14] unable to open database file ... Commented Oct 8, 2024 at 20:18
  • "unable to open database file" means either the file isn't where you're telling it, or permissions aren't correct. Try using the full path like new PDO('sqlite:/srv/http/test/db1.db') (or whatever it is) and maybe chmod go+r it if needed. Commented Oct 8, 2024 at 20:42

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.