0

I've got a working version of the code on my local machine using xampp but when I uploaded it to my Raspberry Pi it seems to go haywire.

Basically I've got an angularjs script calling a file called dbConn.php which is in the same directory and works on a local machine but doesn't when uploaded to a Pi.

function raspberryController($scope, $http, $timeout) {
    var poll = function() {
        $timeout(function() {
            try{
                $http.get("dbConn.php")
                .success(function(response) {$scope.names = response;})
                .error(function(data, status, header, config) {
                    console.log(alert, status, header, config);
                });
            } catch (e) {
                console.log("Got an error!",e);
                throw e; 
                // rethrow to not marked as handled
            }
            poll();
        }, 1000);
    }
    poll();
}

dbConn.php

<?php
//$db = new SQLite3('system/modules/test.db'); 
header("Access-Control-Allow-Origin: *");
try{
    $dbh = new PDO('sqlite:system/test.db') or die("cannot open db");
} catch(Exception $e) {
    echo $e->getMessage(); 
}

$query = 'SELECT * FROM connected;';
$results = $dbh->query($query);

$outp = '[';

foreach($dbh->query($query) as $row){
    if ($outp != "[") {
        $outp .= ",";
    }
    $outp .= ' { "ID":"' . $row[0] .'",';
    $outp .= '"IP":"' . $row[1] .'",';
    $outp .= '"CONNECTED":"' . $row[2] .'",';
    $outp .= '"LEASE_TIME":"' .$row[3] .'" } ';
}

$outp .= ']';

echo($outp);
?>
grg
  • 5,023
  • 3
  • 34
  • 50
Forbsey1
  • 155
  • 8

1 Answers1

1

You have:

try { $dbh = new PDO('sqlite:system/test.db') or die("cannot open db"); } catch(Exception $e) { echo $e->getMessage(); }

You're trying to call the query() function on the $dbh object outside of the try.

Here's a link to variable scoping in PHP: http://php.net/manual/en/language.variables.scope.php

To solve your problem either have an empty $dbh variable outside the try block and then initialise it within the try eg:

$dbh = null;
try {
    $dbh = new PDO('sqlite:system/test.db') or die('cannot open db');
} catch (Exception $e) {
    //some code to handle it in here
    exit(); //to stop anything else from executing
}
$result = $dbh->query();

The other way to solve the problem would be to put the rest of your code in the try/catch block you've created for the initialisation of the $dbh variable.

try {
    $dbh = new PDO('sqlite:system/test.db') or die('cannot open db');
    $query = 'SELECT * FROM connected;';
    $results = $dbh->query($query);

    $outp = '[';
    foreach($results as $row){
        if ($outp != "[") {
            $outp .= ",";
        }
        $outp .= ' { "ID":"' . $row[0] .'",';
        $outp .= '"IP":"' . $row[1] .'",';
        $outp .= '"CONNECTED":"' . $row[2] .'",';
        $outp .= '"LEASE_TIME":"' .$row[3] .'" } ';
    }
    $outp .= ']';
    echo $outp;
} catch (Exception $e) {
    echo $e->getMessage();
}

You'd be better off abstracting DB logic into a separate class and using it like a Singleton or a static class as detailed in this other post: Best practices for a PDO class?.

Oh and for future reference, the easiest way to find out what's going wrong if you're met with a unhelpful err 500 is to use tail -f /var/log/apache2/error.log from your terminal if you're using a Debian distro such as Raspbian.

Community
  • 1
  • 1
waddy
  • 26
  • 2