Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are all data types returned as strings with sqlite3 fetchAll(PDO:FETCH_ASSOC) with PHP?
    primarykey
    data
    text
    <p>I am using</p> <pre><code>$rows = $statement-&gt;fetchAll(PDO::FETCH_ASSOC); </code></pre> <p>to get all the rows of my table.</p> <p>The schema is defined as "id INTEGER PRIMARY KEY, title TEXT, year INTEGER, price REAL"</p> <p>A row from he results of the fetchAll is</p> <pre><code>array(4) { [0]=&gt; string(1) "1" [1]=&gt; string(14) "The Dark Night" [2]=&gt; string(4) "2008" [3]=&gt; string(5) "19.95" } </code></pre> <p>Why are all the data types being returned as strings? I want them to be returned as defined in the schema. I understand the 'typeless' nature of SQLite, but they do define the limited data types as TEXT, INTEGER, and REAL. How can I get the data to be returned with the specified data types? I don't want to iterate through each row, and convert it with PHP - that just seems to be too slow.</p> <p>The Complete Test Code as Follows:</p> <pre><code>&lt;? class sqlite_test { function __construct() { $this-&gt;dbase_filename = "test.sqlite"; $this-&gt;init_dbase(); } function init_dbase() { $this-&gt;pdo = new PDO("sqlite:".$this-&gt;dbase_filename); } function open_table() { $table = "dvds"; if ( ! ($test_to_see_if_table_exists = $this-&gt;pdo-&gt;query("SELECT 1 from $table")) ) { $schema = "id INTEGER PRIMARY KEY, title TEXT, year INTEGER, price REAL"; $query = "CREATE TABLE $table ($schema)"; $this-&gt;pdo-&gt;exec($query); } return $test_to_see_if_table_exists; } function add_test_records() { $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "The Dark Night", 2008, 19.95)'; $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "The Wizard of Oz", 1939, 9.95)'; $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "Jaws", 1977, 6.95)'; $this-&gt;pdo-&gt;exec( join(';', $query) ); } function dump_test_records() { $query = "SELECT * FROM dvds"; if ($statement = $this-&gt;pdo-&gt;prepare($query)) { $statement-&gt;execute(); $rows = $statement-&gt;fetchAll(PDO::FETCH_ASSOC); echo "&lt;pre&gt;"; var_dump($rows); echo "&lt;/pre&gt;"; } } function main() { if ( ! $this-&gt;open_table() ) { $this-&gt;add_test_records(); } $this-&gt;dump_test_records(); } } $test = new sqlite_test(); $test-&gt;main(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload