sqlite query not working with php
by pizzipie from LinuxQuestions.org on (#4X96Y)
hi,
I'm trying to get a list of tables from chosen sqlite database. If I query the database in a console the query returns the tables properly. If I query the database with the same query in PHP I get no return. I have attached views of what is happening. Code is below: HAPPY NEW YEAR
Code:<?php
error_reporting (E_ALL ^ E_NOTICE);
include("../myPhpFunctions.inc"); // contains lf(); sp(); myprint; check_input();
// shows break space print_r on browser
$orgdir=getcwd();
$dir="/home/rick/DB-sql/";
$test=$files=array();
// =====================================================
// change to working directory where the databases are and get the *.db files
chdir($dir);
// get the available db file names
if (is_dir($dir)){
if ($dh = opendir($dir)){ // open dir and read contents
while (($file = readdir($dh)) !== false){
if(substr($file, -2)=="db") {
$files[]=$file;
}
}
}
}
closedir($dh);
// =====================================================
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="#" method="post">
<p>Databases Available</p>
<!-- <select multiple="multiple" name="database[]"> to pick multiple choices-->
<select name="database" size="5">
<!-- <option value=""></option> first line blank -->
<?php
foreach($files as $file) {
echo "<option value=".$file.">".$file."</option>".lf();
} // foreach
?>
</select>
<p><input type="submit" name="submit" value="Choose Database" /></p>
</form>
<br />
</body>
</html>
<?php
if(isset($_POST['submit'])){
$dbase=$_POST['database'];
// ======================================================================
// Connect to Database and SELECT tables for Dropdown list
// =====================================================================
class MyDB extends SQLite3 {
function __construct() {
$this->open("'".$dbase."'");
//$this->open('/home/rick/Desktop/sqliteDatabases/renoAZID.db');
}
}
$db = new MyDB();
if(!$db) {
echo $db->lastErrorMsg();
} else {
echo "Opened database".sp().$dbase.sp()." successfully".lf();
}
$tablesquery = $db->query("SELECT distinct tbl_name from sqlite_master order by 1;");
$table = $tablesquery->fetchArray();
myprint($table); bye(101); <===== this is print_r with <pre> attached -- bye is exit()
echo '<pre>'.$table['tbl_name'] . '</pre>';
$db->close();
} // isset $_POST
chdir($orgdir);
?>
Attached Thumbnails


I'm trying to get a list of tables from chosen sqlite database. If I query the database in a console the query returns the tables properly. If I query the database with the same query in PHP I get no return. I have attached views of what is happening. Code is below: HAPPY NEW YEAR
Code:<?php
error_reporting (E_ALL ^ E_NOTICE);
include("../myPhpFunctions.inc"); // contains lf(); sp(); myprint; check_input();
// shows break space print_r on browser
$orgdir=getcwd();
$dir="/home/rick/DB-sql/";
$test=$files=array();
// =====================================================
// change to working directory where the databases are and get the *.db files
chdir($dir);
// get the available db file names
if (is_dir($dir)){
if ($dh = opendir($dir)){ // open dir and read contents
while (($file = readdir($dh)) !== false){
if(substr($file, -2)=="db") {
$files[]=$file;
}
}
}
}
closedir($dh);
// =====================================================
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="#" method="post">
<p>Databases Available</p>
<!-- <select multiple="multiple" name="database[]"> to pick multiple choices-->
<select name="database" size="5">
<!-- <option value=""></option> first line blank -->
<?php
foreach($files as $file) {
echo "<option value=".$file.">".$file."</option>".lf();
} // foreach
?>
</select>
<p><input type="submit" name="submit" value="Choose Database" /></p>
</form>
<br />
</body>
</html>
<?php
if(isset($_POST['submit'])){
$dbase=$_POST['database'];
// ======================================================================
// Connect to Database and SELECT tables for Dropdown list
// =====================================================================
class MyDB extends SQLite3 {
function __construct() {
$this->open("'".$dbase."'");
//$this->open('/home/rick/Desktop/sqliteDatabases/renoAZID.db');
}
}
$db = new MyDB();
if(!$db) {
echo $db->lastErrorMsg();
} else {
echo "Opened database".sp().$dbase.sp()." successfully".lf();
}
$tablesquery = $db->query("SELECT distinct tbl_name from sqlite_master order by 1;");
$table = $tablesquery->fetchArray();
myprint($table); bye(101); <===== this is print_r with <pre> attached -- bye is exit()
echo '<pre>'.$table['tbl_name'] . '</pre>';
$db->close();
} // isset $_POST
chdir($orgdir);
?>
Attached Thumbnails