How to execute PDO in a variable PHP

Multi tool use
Multi tool use


How to execute PDO in a variable PHP


while($row =$result->fetch())
{
$output .= '
<tr>
<td>' . $row["ID_personne"] . '</td>
<td>' . $row["Nom"] . '</td>
<td>' . $row["Prenom"] . '</td>
<td>' . $row["Telephone"] . '</td>
<td>' . $row["Mail"] . '</td>
<td>' . $row["Categorie"] . '</td>
<td>' . $row["Type"] . '</td>
'.$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$q2->bindValue(":id",$row["Entretien"]);
$q2->execute();
$entretien=$q2->fetch().'

<td>'.$entretien["Date"].'</td>
<td width=250>
<a class="btn btn-primary" href="update_personnes.php?ID_personne='.$row["ID_personne"].'"><span class="glyphicon glyphicon-pencil"></span> Modifier</a>

<a class="btn btn-danger" href="delete_personnes.php?ID_personne='.$row["ID_personne"].'"><span class="glyphicon glyphicon-remove"></span> Supprimer</a>
</td>
</tr>';
}
$output .= '</table>';
echo $output;



I have always an Error: ( ! ) Catchable fatal error: Object of class PDOStatement could not be converted to string in C:.......... on line 61('.$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$row["Entretien"] it's a foreign key





Wouldn't it be easier to just use a JOIN inside the original query and retrieve all data at once? It seems you'll issue lots of queries currently depending on the size of the original query.
– Sirko
Jul 3 at 8:42


JOIN





You'd do yourself a lot of favours by formatting your code and making it neater when you write it.
– Script47
Jul 3 at 8:45




1 Answer
1



There was some syntax error in your code as $q2 is being concat with html string. Improved code should be:


$q2


while ($row = $result->fetch()) {
$output .= '
<tr>
<td>' . $row["ID_personne"] . '</td>
<td>' . $row["Nom"] . '</td>
<td>' . $row["Prenom"] . '</td>
<td>' . $row["Telephone"] . '</td>
<td>' . $row["Mail"] . '</td>
<td>' . $row["Categorie"] . '</td>
<td>' . $row["Type"] . '</td>';
$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$q2->bindValue(":id", $row["Entretien"]);
$q2->execute();
$entretien = $q2->fetch();

$output .= '<td>' . $entretien["Date"] . '</td>
<td width=250>
<a class="btn btn-primary" href="update_personnes.php?ID_personne=' . $row["ID_personne"] . '"><span class="glyphicon glyphicon-pencil"></span> Modifier</a>

<a class="btn btn-danger" href="delete_personnes.php?ID_personne=' . $row["ID_personne"] . '"><span class="glyphicon glyphicon-remove"></span> Supprimer</a>
</td>
</tr>';
}
$output .= '</table>';
echo $output;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

R3Q Uju9y,UAejhmPf8JRFoS97BI lfMw27 2AZfgntwjG0Q0s5,D Z,1lXcsr8MWzSsIEeJ97sZsEKKZ,7vnx
FERxZwiR,Dx QYHzV0RuCX1xi1 O361bl2OMBJ,pDeQGISWC,VV KCjppz CeW

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications