Article 55HP1 SQLite3 - PHP Fatal error: Uncaught Error: Call to a member function exec()

SQLite3 - PHP Fatal error: Uncaught Error: Call to a member function exec()

by
pizzipie
from LinuxQuestions.org on (#55HP1)
I have been trying to sort a SQLite3 database on a 'Date' (type=text) Column. No matter what format I use "07/09/2020" or "2020-07-09" The database will not sort it accurately. Because of this I am going to try using julian days (type=real)

So in trying to Update SQLite3 database I get a Fatal error.
Don't have a clue how to interpret this or fix it.

The code below is the PHP program which will create the database, create and populate the table and attempt to update the table.

I want to run this in Firefox after this bug is fixed!

Code:<?php
// file /var/www/rickSQL.com/public_html/renoAZID/aatest00.php
// Thu Jul 9, 11:04

error_reporting (E_ALL ^ E_NOTICE);

global $db;

class MyDB extends SQLite3 {
function __construct() {
$this->open('test00.db');
}
}
$db = new MyDB();
if(!$db) {
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}

"DROP TABLE IF EXISTS `testdata`";

$query=
"CREATE TABLE IF NOT EXISTS `testdata` (
`Id` INTEGER PRIMARY KEY AUTOINCREMENT,
`Date` TEXT NOT NULL,
`Location` TEXT NOT NULL,
`Description` TEXT NOT NULL,
`Category` TEXT NOT NULL,
`Amount` REAL
);";

$ret = $db->exec($query);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo "Table created successfully\n";
}


$query=
"INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('04/03/2019','419 Upper Blvd', 'Kitchen Sink And Faucet','appliances',438.5);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('04/21/2019','419 Upper Blvd','Refrigerator And BR Light','appliances',1567.55);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('05/03/2019','419 Upper Blvd','Washer Dryer','appliances',1102.1);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('05/17/2019','419 Upper Blvd','Kitchen Lights','appliances',113.67);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('04/01/2019','419 Upper Blvd','Kitchen Fixtures Stove - Ect','appliances',862.82);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('04/26/2019','419 Upper Blvd','House Cleanup','cleaning', 130);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('11/12/2018','419 Upper Blvd','Remove Wallpaper','demo', 650);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('11/19/2018','419 Upper Blvd','Remove Sinks And Toilets - Baths','demo',206.85);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('11/19/2018','419 Upper Blvd','Gas In Kitchen - Off- Rm Lr Heater','demo',99);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('06/01/2019','419 Upper Blvd','Misc','tools',26.35);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('06/13/2019','419 Upper Blvd','Misc','tools',84.79);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('09/24/2019','419 Upper Blvd','Misc','tools',19.58);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('10/29/2019','3607 Smithfield','Service-New Rollers-Lube','maint', 208.2);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('03/01/2019','3607 Smithfield','Seasonal Inspection','maint',39.95);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('10/16/2020','3607 Smithfield','New Remote and Estimate for seals','maint',46.84);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('06/01/2020','419 Upper Blvd','Sprinkler Turn-on; Backflow Test','landscape',65.0);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('04/10/2020','419 Upper Blvd','Fertilize & Weed Control','landscape','0.00');
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('06/21/2020','419 Upper Blvd','Fertilize & Weed Control - paid for April also','landscape',126.0);
INSERT INTO `testdata` (Date,Location,Description,Category,Amount) VALUES ('10/16/2018','3607 Smithfield','New Remote and Estimate for seals','maint', 46.84);
";

$ret = $db->exec($query);
if(!$ret) {
echo $db->lastErrorMsg();
} else {
echo "Records created successfully\n";
}
// ================================================================================================

// UPDATE DATES HERE

// ================================================================================================

$query="SELECT * FROM testdata;";

$ret = $db->query($query); // FATAL ERROR POINTS TO THIS
while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {
$rows[]=$row;
}
echo "Operation done successfully\n";

for($i=0; $i<count($rows); $i++) {
$val=$rows[$i]['Date'];
$index=(int)$rows[$i]['Id'];
$x=explode("/",$val);
updateDb($index, $x);

/* OUTPUT INCLUDING THE ERROR MESSAGE

Opened database successfully
Table created successfully
Records created successfully
Operation done successfully

PHP Fatal error: Uncaught Error: Call to a member function exec() on null in /var/www/rickSQL.com/public_html/renoAZID/aatest00.php:136
Stack trace:
#0 /var/www/rickSQL.com/public_html/renoAZID/aatest00.php(84): updateDb(1, Array)
#1 {main}
thrown in /var/www/rickSQL.com/public_html/renoAZID/aatest00.php on line 136

*/

}

//print_r($rows); exit("Leaving Update area - Line 75\n\n");

// ================================================================================================

// THIS SECTION WILL NOT RUN - HELP

$query="SELECT * FROM testdata;";

$ret = $db->query($query);
while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {
$rows[]=$row;
}
echo "Operation done successfully after update \n";

$db->close();

print_r($rows);

// ============================== FUNCTIONS HERE =============================

function updateDb($index, $val) { // update database to julian dates

$x=gregoriantojd($val[0], $val[1], $val[2]);

$query="UPDATE testdata SET `Date`= ".$x." WHERE `Id`= ".$index.";";

//exit($query.".... bye from line 131 \n\n");

// $query = UPDATE testdata SET `Date`= 2458577 WHERE `Id`= 1;

$ret = $db->exec($query);
if(!$ret) {
echo $db->lastErrorMsg();
} else {
echo $db->changes(), " Dates updated successfully\n";
}
} // update()

?>latest?d=yIl2AUoC8zA latest?i=1awHw8RqylM:acMz5Ig45cg:F7zBnMy latest?i=1awHw8RqylM:acMz5Ig45cg:V_sGLiP latest?d=qj6IDK7rITs latest?i=1awHw8RqylM:acMz5Ig45cg:gIN9vFw1awHw8RqylM
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments