php - How to properly utilize PDO? -


this question has answer here:

quick edit: although says mamp, store web projects there , not utilize mysql server provides (though when tried threw same error), use mysql came mysql workbench*

in php code working on connect database follows:

$dbh = new pdo('mysql:host=localhost:3306;dbname=mydb', $user, $pass); 

now, tested user , password database both via terminal (which connects successfully), along interface ide (phpstorm) provides connects.

my issue follows: how past following error:

fatal error: uncaught error: call member function query() on null in /applications/mamp/htdocs/gis/4musketeers/php/common.php:20

now know mentions line 20 refers start of foreach block:

function printdirectors($sqlquery) {     $index = 0; //counting index our table     global $dbh; //this how refer our global $dbh top.      foreach ($dbh->query($sqlquery) $result) {         echo "<tr><td class=\"index\">";         echo $index + 1 ."</td>";         echo "<td class=\"firstname\">";         echo $result['first_name'] ."</td>";         echo "<td class=\"lastname\">";         echo $result['last_name'];         echo "</td></tr>";         $index++;     } } 

i quite confident query correct provide me result interested in. query being:

select distinct directors.first_name, directors.last_name directors, actors directors.first_name = actors.first_name , directors.last_name = actors.last_name order directors.first_name, directors.last_name; 

and result being:

first_name     last_name andrew         adamson guy            ritchie james (i)      cameron jay            levey mel (i)        gibson 

this first time have use pdo or sql php, advice highly appreciated.

as output states, reason getting because never instantiated $dbh. php tries call query method on it, null.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -