SQLite Forum

Select Query will not work in PHP program
Login

Select Query will not work in PHP program

(1) By rp (pizzipie) on 2020-04-09 01:21:21 [link] [source]

Hi, Using Ubuntu 18.04. I have a sqlite query in a PHP 7+ program that will not respond. The exact same query works fine if I use the .commands. Here is my code taken from https://www.tutorialspoint.com/sqlite/sqlite_php.htm  => SELECT Operation Following PHP program shows how to fetch and display records from the COMPANY table created in the above example − and modified to fit my case. I am very new to Sqlite so I realize I have probably done something really dumb. Thanks in advane for your help.

Sorry - I don't know how to format this Post so it doesn't look like garbage!! R

// ====================  database =============================

   class MyDB extends SQLite3 {       function __construct() {          $this->open($database); // optumRx.db       }    }    $db = new MyDB();    if(!$db) {       echo $db->lastErrorMsg();    } else {       echo "Opened database successfullyn";        //  this shows in display    }    $sql ="SELECT * from rxdata";

   $ret = $db->query("SELECT * FROM rxdata");           // tried $sql here - didn't work either    while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {       echo "AcctFor = ". $row['AcctFor'] . "n";       echo "Invoice Date = ". $row['InvDate'] ."n";          // I get no PHP warnings or errors here       echo "Order no = ". $row['OrderNo'] ."n";       echo "Drug No = ".$row['DrugNo'] ."n";       echo "Description = ".$row['Description'] ."n";       echo "Cost = ".$row['Cost'] ."n";       echo "Refills = ".$row['ReFills'] ."nn";    }    echo "Operation done successfullyn";   // does not show in display    $db->close();

(2) By J. King (jking) on 2020-04-09 01:31:49 in reply to 1 [link] [source]

$database in the constructor is undefined. If you set your error_reporting() to show notices, PHP will advise you of this problem (and probably others).

(3) By rp (pizzipie) on 2020-04-09 01:58:36 in reply to 2 [link] [source]

Thanks jking,

The first few lines of my code are as follows:

<?php

error_reporting (E_ALL ^ E_NOTICE);

include('../include/myPhpFunctions.inc');

$database="optumRx.db"; $insertVals=""; $norecords=0; $noInserts=0;

I have this area where I use declarations (like in "C") so I don't think $database was undefined. However I did use the string "optumRx.db" in place of the variable $database and all is well. I don't understand why a variable will not work here!!

(4) By anonymous on 2020-04-09 02:13:17 in reply to 1 [source]

If you want to format this post so it doesn't look like garbage, there are two ways: Either to select "Plain Text" from the "Markup style" menu (above the text entry), or you can write ```php on a line by itself above the PHP code, and ``` on a line by itself below the PHP code.

(5) By rp (pizzipie) on 2020-04-09 16:16:52 in reply to 4 [link] [source]

```php<?php

errorreporting (EALL ^ E_NOTICE);

include('../include/myPhpFunctions.inc');

$database="optumRx.db"; $insertVals=""; $norecords=0; $noInserts=0;
```

doesn't look any different to me except "<?php" is added

R

(6) By Kees Nuyt (knu) on 2020-04-09 16:27:34 in reply to 3 [link] [source]

At first sight, I would say:

$database

is defined, but not in the context of the constructor.

To make it visible to the constructor, try code like this:


// ===database ===

  class MyDB extends SQLite3 {
    function __construct() {
      global $dataabse;
      $this->open($database); // optumRx.db
    }
  }
-- 
Regards,
Kees Nuyt

(7) By rp (pizzipie) on 2020-04-09 17:04:34 in reply to 5 [link] [source]

Thanks for the reply,

I used "global $database" and it works fine now.

R

(8) By Gonzalo Diethelm (gonzus) on 2020-04-09 19:47:08 in reply to 5 [link] [source]

You need a newline after ```php:

```php
$database="foo.db";
```

will render as:

$database="foo.db";