A
Anonymous
Guest
Trying to update data base and am getting FATAL error. Don't have a clue how to decipher the message.
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);
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);
[b]/* 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
*/ [/b]
}
//print_r($rows); exit("Leaving Update area - Line 75\n\n");
// ================================================================================================
$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); [b]// THIS WILL NOT RUN[/b]
// ============================== 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.";";
// $query = UPDATE testdata SET `Date`= 2458577 WHERE `Id`= 1;
//exit($query.".... bye from line 131 \n\n");
$ret = $db->exec($query);
if(!$ret) {
echo $db->lastErrorMsg();
} else {
echo $db->changes(), " Dates updated successfully\n";
}
} // update()
?>