我想在 accueil.php
中包含 functions.php
中的函数,但它不起作用。
查看我的 html 代码:
<div class="milieudepage">
<table>
<tr>
<th> Equipe 1 </th>
<th> Equipe 2 </th>
<th> Drapeau 1 </th>
<th> Drapeau 2 </th>
<th> Score 1 </th>
<th> Score 2 </th>
<th> Buvettes </th>
<th> Volontaires </th>
</tr>
<?php
include("functions.php");
AfficheMatch();
?>
</table>
</div>
在functions.php
中查看我的php代码:
<?php
include 'connect.php';
function AfficheMatch()
{
echo "coucou";
$req = $bdd->prepare("SELECT * FROM Equipe");
$req->execute();
while($donnees = $req->fetch())
{
echo '<tr><th>'$donnees['pays']'</th><th>'$donnees['drapeau']'</th></tr>';
}
}
?>
最佳答案
如果看不到代码的其余部分,很难说,但我会尝试一下:
function AfficheMatch()
{
global $bdd;
echo "coucou";
$req = $bdd->prepare("SELECT * FROM Equipe");
$req->execute();
while($donnees = $req->fetch())
{
echo '<tr><th>'.$donnees['pays'].'</th><th>'.$donnees['drapeau'].'</th></tr>';
}
}
问题 1:原始代码中存在语法错误,变量正好与字符串相对应,例如 '<code>'$var
。这是行不通的,PHP 要求您使用 .
连接字符串和/或变量。运算符(operator)。所以它会是 '<code>'.$var
.
问题2:需要引用全局变量$bdd
与 global
函数内的关键字以便访问它。
关于当我想在 HTML 中执行 PHP 函数时,PHP 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703631/