Would you know why my query does not return any value after echo res:
? (res:
appears well, but nothing after).
I have got one table named Person, and a JOIN table named people_friends :
CREATE TABLE person (
idperson integer primary key,
name VARCHAR(50)
);
CREATE TABLE people_friends (
person_id integer,
otherPerson_id integer,
primary key (person_id, otherPerson_id),
FOREIGN KEY (person_id)
REFERENCES person (idperson),
FOREIGN KEY (otherPerson_id)
REFERENCES person (idperson));
And the query :
<?php
//CONNECT
try {
$connection = new PDO('pgsql:host='.$PARAM_host.';dbname='.$PARAM_db_name, $PARAM_user, $PARAM_password);
echo "test";
}
catch(Exception $e)
{
echo 'Error : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
//QUERY
try {
echo '<br/>qsdf<br/>';
$q = "SELECT p.idperson, p.name FROM person p INNER JOIN people_friends pf ON p.idperson = pf.person_id";
$res = $connection->query($q);
$res->setFetchMode(PDO::FETCH_ASSOC);
echo "res : ".$res->name;
}
catch(Exception $e)
{
echo 'Error : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
?>
Thanks
You missed to fetch records. It should be:
...
$res->setFetchMode(PDO::FETCH_ASSOC);
while($record = $res->fetch()) {
var_dump($record['name']);
}
...