1

I have an SQLite database made using the SQLite port in Sencha Touch (here) and the database name is "cars" without the quotes and the table inside it is called "cars_table" without the quotes.

I am able to read/write/update the SQLite table perfectly fine in javascript and when I look at the "Cookies/Local Storage" page on google chrome I see that I have the database under my domain set with the correct database name.

I am however completely unable to read it or write to it from PHP. I have PHP 5.3 and when checking phpinfo() I have SQLite ready to go.

Any ideas?

My basic PHP code is just:

$dbhandle = sqlite_open('cars');
$query = sqlite_query($dbhandle, 'SELECT * FROM cars_table');
$rows = sqlite_num_rows($query);
echo $rows;

Errors in PHP:

Warning: sqlite_query(): no such table: cars_table in     /var/www/manage/testall/www/thesqlite.php on line 6 Warning: sqlite_num_rows() expects parameter 1 to be resource, boolean given in /var/www/manage/testall/www/thesqlite.php on line 7

Edit 2:

When searching to list the table names using sqlite_master I am seeing 0 tables. So the error could be in the database name (in which case I am missing how the sencha touch app is naming it).

Calvin
  • 309
  • 2
  • 5
  • 18
  • 1.) what actually happens when you try to use php ? and 2.) you sure you use javascript and php on the same machine ? – arkascha Jul 13 '12 at 15:58
  • What results or error messages are you getting when you run that code? – Jazz Jul 13 '12 at 15:58
  • No errors or results. Purely a blank page. Both the javascript and php are hosted on the same server in the same folder (with full read/write permissions). – Calvin Jul 13 '12 at 16:02
  • to display errors, add this code at the beginning of your PHP code: error_reporting(E_ALL); ini_set('display_errors', 1); – Jocelyn Jul 13 '12 at 16:24
  • thanks! the errors have been edited in to the bottom of the post – Calvin Jul 13 '12 at 16:28
  • the query fails as seen on error because there is not table in database, are you sure connection to database succeeds ? – Gntem Jul 13 '12 at 16:48
  • I am not sure if connection to the database succeeds at all. Very new with sqlite and wasn't sure what was needed to connect besides the simple sqlite_open() line. – Calvin Jul 13 '12 at 16:50

2 Answers2

1

I will walk the reverse path on errors and the code you provided:

Warning: sqlite_query(): no such table: cars_table in /var/www/manage/testall/www/thesqlite.php on line 6 Warning: sqlite_num_rows() expects parameter 1 to be resource, boolean given in /var/www/manage/testall/www/thesqlite.php on line 7

Means that $query is a boolean (false in this case) instead of a resource.

$query is false because sqlite_query($dbhandle, 'SELECT * FROM cars_table') is a failed query because cars_table do not exists in the database.

cars_table do not exists in the database because the dababase was just created, (yes, just created) by your sentence sqlite_open('cars') so the db has no tables. This is the default behavor, as you mention in one of your coments. cars is a non-existant file on the server (where php runs) because the good cars file/database is located on client side (the browser), where javascript can reach and operate it.

but, careful, you can find a cars file/database in your server, because SQLite creates it empty each time it is not found.

if you need to use the genuine cars file/database you need to upload to the server first.

Saic Siquot
  • 6,513
  • 5
  • 34
  • 56
  • thanks! I guess I was treating the sqlite more like a cookie file where it is on the client side but can be read by PHP from the server. I guess I will need to find a way to iterate through the sqlite on the client side and upload to server and vice versa. – Calvin Jul 13 '12 at 17:49
0

Edited
table a look here How to list the tables in an SQLite database file that was opened with ATTACH?

if the table doesn't exist then you can execute any query on non-existed tables, therefore you get FALSE.

Community
  • 1
  • 1
Gntem
  • 6,949
  • 2
  • 35
  • 48
  • I believe that sqlite_open will automatically create the database if one is not available already...so it will always bring up "Database is opened". Even with completely random database names such as "asaskfjdsnkf". (I could be wrong, but it was showing is opened for everything I tested). – Calvin Jul 13 '12 at 16:59