0

I'm porting over a small php app to use sqlite, and this is the first time that I use sqlite with php and its been a while since i last coded something with php(it was php4 by the time). The trouble i'm having right now is that apparently with sqlite you need to call the sqlite3 object several times since it apparently doesnt establish a permanent connection with the database, so my code right now is filled with

$db = new SQLite3('test.db');

in every single function in the code. is this efficient? is there a better and cleaner way of doing this?

the other issue that i'm having is if i use sqlite inside say include/functions.php it tries to look for test.db inside this include/ dir when it should be using the app root. how could i fix this efficiently?

6
  • "since it apparently doesn't establish a permanent connection with the database" - Why would you ever think this? Commented Dec 5, 2010 at 23:59
  • @Dark Falcon: since the $db object has to be called several times in the code. i remember with mysql you would just connect once and the session was active until you closed it. Commented Dec 6, 2010 at 0:01
  • And so it should also be with Sqlite. Sure you're not doing something wrong? Commented Dec 6, 2010 at 0:07
  • @Dark Falcon: well, the thing is i don't know how to make the $db object to be seen by all the functions and php files in my project. i could make it a global variable, but i've read thats not safe and a good practice with php, so im curious to listening to a more efficent way of resolving this. Commented Dec 6, 2010 at 0:11
  • There is nothing wrong with using a global variable for something like a database connection. Perhaps what you've heard as being dangerous is using register_globals, which is indeed unwise. Commented Dec 6, 2010 at 0:13

2 Answers 2

1

You only have to establish a connection again if your $db variable goes out of scope or is otherwise destroyed. If you create the variable inside a function, it will go out of scope as soon as the function exits, forcing you to recreate the variable/connection in the next function.

As such, in some form or another, you will need to create the variable in a scope that is accessible everywhere a database connection is required. The easiest but ugliest is to create it in the global scope. A better way would be to create a static class that holds the connection and can return the connection handler on request. Another way would be to use dependency injection, basically meaning you pass the connection handle into every function or object where it is needed.

The right answer depends on your existing architecture.

Sign up to request clarification or add additional context in comments.

Comments

0

Code

<?php
class SQL {
    private $db;

    function __construct() {
        try {
           /*** connect to SQLite database ***/
            $db = new PDO("sqlite::memory:");
            $db->exec("CREATE TABLE blog (Id INTEGER PRIMARY KEY, text TEXT)");   
            $this->db = $db;
            /*** a little message to say we did it ***/
            echo 'database created in memory';
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }

    function add($text) {
        $this->db->exec("INSERT INTO blog(text) VALUES ('$text')");
    }

    function get() {
        $res = array();
        $result = $this->db->query('SELECT text FROM blog');
        foreach ($result as $row) {
            $res[] = $row['text'];
        }

        return $res;
    }
}
?>

<?php
$sql = new SQL();
$sql->add('hello');
print_r($sql->get());
?>

<?php
$sql = new SQL();
$sql->add('hello');
$sql->add('world');
print_r($sql->get());
?>

Output:

php sql.inc 

database created in memoryArray
(
    [0] => hello
)

database created in memoryArray
(
    [0] => hello
    [1] => world
)

As you can see no sprinkling off $db =.

P.S: You should use PDO because it is superior to just calling new SQLite3

Comments

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.