0

I'm running a small web server on my raspberry pi using nginx, php5, and sqlite3. Unfortunately, when I'm trying to insert some data into my database, it just stops at sqlite_open(). No errors, it just stops.

<html>
<head>
<title>halp</title>
<style>
* {font-family:verdana;}
</style>
</head>
<body>
<p><h2>Log in</h2></p>
<form action="" method="POST">
<table>
<tr><td>Username</td><td><input type="text" name="user"></td></tr>
<tr><td>Password</td><td><input type="password" name="pass"></td></tr>
<tr><td><input type="submit" value="Submit"></td><td><input type="checkbox" value="r" name="r"></td></tr>
</table>
</form>
<?php
if(isset($_POST['r']) || isset($_POST['user'])){
$reg = $_POST['r'];
if($reg == 'r'){
//      echo $reg, "<br>";
        echo "Registering... <br>";
        $user = md5($_POST['user']);
        $pass = md5($_POST['pass']);
//      echo "user and pass <br>";
//      echo "user: ", $user, " pass: ", $pass, "<br>";
        try{
//              echo "inside try <br>";
                $db = sqlite_open('testdb', 0666, $error);    //stops here
                echo "error: ", $error->getMessage();
        } catch (Exception $e) {
                echo "error: ", $e->getMessage();
        }
//      echo "sqlite_open <br>";
        if(!$db) { die($error); }
        $query = "INSERT INTO users VALUES('$user', '$pass')";
        $registered = sqlite_query($db, $query);
//      echo "sqlite_exec";
        if($registered){
                echo "New user registered.<br>";
//              echo $user."        ";
//              echo $pass;
        } else { die($error); }
        sqlite_close($db);
}
else{
        $user = md5($_POST['user']);
        $pass = md5($_POST['pass']);
//      print $user."\n";
//      print $pass."\n";
        $db = new SQLite3('testdb');
        $results = $db->query("SELECT * FROM users WHERE user = '$user' AND pass = '$pass';");
        if(!$results){ echo 'Database error'; }
        else {
                $resultArray = $results->fetchArray(SQLITE3_NUM);
                if($resultArray[0] == $user && $resultArray[1] == $pass){
                        echo 'Password and username match found!';
                }
                else{
                        echo 'Password and username do not match.';
                }
        }
}
}
?>
<br>
<a href="index.html">Back</a>
</body>
</html>

Once it gets to sqlite_open() it stops. As you can see, there is a link to index.html at the bottom of the code, but when I run this file, it does not display. The echos that I have commented out tell me that I only get as far as the try statement.

I have tried several things to resolve this:

  • Creating a database using sqlite3 in command line
  • Messing with nginx & php configs
    • Specifically, using my own sites-enabled
    • Changing the value of sqlite.assoc_case in php.ini. Frustratingly, there is a link to a page on php.net that does not exist.
  • Because sqlite_open() creates a database if it is not found, I used that to create testdb
  • chmod a+rwx testdb
  • chmod a+rwx /var/www/html
  • Adding myself to www-data group
  • chgrp -R www-data /var/www/html
  • Different browsers
    • IE hates it
  • Rebooting even

This wouldn't be so rage inducing if I at least had an error message. gah.

2 Answers 2

3

sqlite_open() only supports sqlite2. Use PDO to access sqlite3 databases:

$dbh = new PDO('sqlite:testdb');

$stmt = $dbh->prepare("INSERT INTO users VALUES(':user', ':pass')");
$stmt->bindParam(':user', $user);
$stmt->bindParam(':pass', $pass);
$stmt->execute();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This is just an issue of finding the docs confusing, then. On the page for sqlite_open(), someone had pointed out that it was only for sqlite2, but they were totally buried by downvotes.
0

You should know that PHP has a dedicated package for SQLite3 very cleverly named 'SQLite3'. Check out the PHP manual entry. http://php.net/manual/en/book.sqlite3.php

Here's some code snippets that should help.

$db = new SQLite3.php('testdb');
$results = $db->query($query);
while($row = $results->fetchArray()){
    var_dump($row);echo "<br/>";
}
$db->close();

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.