php - Query return and empty array -


the following script:

<?php try  {     $db = new pdo("sqlite:./path/phrases");     $result = $db->query('select * phrases');      foreach($result $row){         $row['phrase'];         $row['score'];     }     } catch(pdoexception $e) {     echo $e->getmessage(); } ?> 

is returning:

warning: invalid argument supplied foreach() in myscript.php on line 5 

if execute: select * phrases; in sql browser, long list of results, regard columns phrase , score. doing wrong?

there 2 examples in answer.

the first example found @ http://juanmanuelllona.blogspot.ca/

am providing hope solution.

1)

try { $db = new pdo("sqlite:./path/phrases");  echo 'database open'; $sql = "select * phrases"; $obj= $db->query($sql) ;  foreach ($obj $row) { print('phrase ='.$row['phrase'].' course='.$row['score']. '&ltbr/>'); // or <br/> } } catch(pdoexception $e) { echo $e->getmessage(); } 

2)

and suggestion taken example @ http://www.phpro.org/tutorials/introduction-to-php-pdo.html

/*** mysql hostname ***/ $hostname = 'localhost';  /*** mysql username ***/ $username = 'username';  /*** mysql password ***/ $password = 'password';  try {     $dbh = new pdo("mysql:host=$hostname;dbname=phrases", $username, $password);     /*** echo message saying have connected ***/     echo 'connected database<br />';      /*** sql select statement ***/     $sql = "select * phrases";     foreach ($dbh->query($sql) $row)         {         print $row['phrase'] .' - '. $row['score'] . '<br />';         }      /*** close database connection ***/     $dbh = null; } catch(pdoexception $e)     {     echo $e->getmessage();     } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -