Lorenzo
Lorenzo

Reputation: 3387

Database path on raspberry

I'm new in raspberry and I'm trying to read a simple database from a php page but there is something wrong: I can't read the database content: Here the php page code:

<!DOCTYPE html>
<html>
<body>
<H1>Test Database</H1>

<?php

$sq = sqlite_open('miodatabase.db', 0666, $sqlite_error);

if(!$sq)
{
   die(“Errore Sqlite: “.$sqlite_error);
}

$result = sqlite_query($sq, 'select * from test');
while($data = sqlite_fetch_array($result))
{
   echo $data[‘nome’];
}
sqlite_close($sq);
?>

</body>
</html>

The database is "miodatabase" that contains a table called "test". I put the database in \var\www\html folder (is correct?) but when I open the page I see a blank page. I'm sure the database contains the table (tested with sqlite3 commands) and the table contains one row. Where I need to put the database? Why I can't see nothing? Thanks

Upvotes: 0

Views: 338

Answers (2)

Lorenzo
Lorenzo

Reputation: 3387

I solved: I type again the command

sudo apt-get install php5-sqlite
sudo /etc/init.d/apache2 restart

and then I can see in the php info the sections about sqlite3. The I update my php page like this:

//Enable show error
ini_set('display_errors', 'On');
error_reporting(E_ALL|E_STRICT);


$db = new SQLite3("miodatabase.db");

$sql_select='SELECT * FROM test'; 
$result=$db->query($sql_select); 

echo "<table border='1'>"; 
echo "<tr>"; 
$numColumns=$result->numColumns(); 
for ($i = 0; $i < $numColumns; $i++) 
{ 
    $colname=$result->columnName($i); 
    echo "<th>$colname</th>"; 
} 
echo "</tr>"; 
while($row = $result->fetchArray(SQLITE3_NUM)) 
{ 
    echo "<tr>"; 
    for ($i = 0; $i < $numColumns; $i++) 
    { 
        $value=$row[$i]; 
        echo "<th>$value</th>"; 
    } 
    echo "</tr>"; 
} 
echo "</table>";

To open the database I use now

$db = new SQLite3("miodatabase.db");

because the sqlite_open is not supported by this version of sqlite. Now all works correctly

Upvotes: 0

KerooZ
KerooZ

Reputation: 61

If you don't see anything, i think it's a php configuration issue.

Did you try to add :

  <?php  phpinfo(); ?>

at the beginning of your script. If you have blank page, look your apache/nginx configuration.

You can also try to run your script from the command line. Maybe helpful in some case.

UPdate

If you use SQLite3 follow this code

class MyDB extends SQLite3
{
    function __construct()
    {
        $this->open('miodatabase.db');
    }
}

$db = new MyDB();
$result = $db->query('select * from test', SQLITE3_OPEN_READWRITE );

//var_dump($result->fetchArray());
while($data = $result->fetchArray())
{
   echo $data[‘nome’];
}
$db->close();

Upvotes: 1

Related Questions