我正在尝试使用数据库中的数据填充表,而PDO中没有运气。我是PHP的新手,所以我一直在搜索并找到解决方案。该表根本没有填充,没有数据。这是我的代码。
$stmt = $dbh->prepare("SELECT * FROM actividades");
$stmt->execute();
echo "<table id=\"premiacionguaynabo\"> <tr> <th> No. </th> <th> Fecha </th> <th> Torneo </th> <th> Lugar </th> <th> Organizador </th> <th> Opciones </th> </tr>"; //The table and the headers are created
$tno = 0;
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
foreach($result as $line) {
$tno = $tno + 1;
$id = $line["idactividades"];
print "<tr class=\"alt2\">";
print "<td id=\"idtorneo\"> $tno </td>";
print "<td class=\"fechatorneo\"> " . $line['fecha_inicial'] . " al " . $line['fecha_final'] . "</td>";
print "<td> <a id=\"plinks\" href=\"$picture\" rel=\"lightbox\" target=\"_top\" title=\"Flyer del Torneo\"> " . $line['ntorneo'] . " </a></td>";
print "<td>" . $line['ltorneo'] . "</td>";
print "<td>" . $line['otorneo'] . "</td>";
print "<td id=\"idtorneo\"> <a id=\"udlinks\" href=\"uactividades.php?idactividades=$id\"> Edit </a> <a id=\"udlinks\" onclick=\"return confirmDelete()\" href=\"dactividades.php?idactividades=$id\"> Delete </a></td>";
print "</tr>";
}
print "</table>";
以下是我连接数据库的方式
$dbhost = 'myhost';
$dbname = 'dbname';
$dbuser = 'myuser';
$dbpass = 'mypass';
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
答案 0 :(得分:0)
尝试更改
foreach($result as $sline) {
到
foreach($result as $line) {
在那种情况下,这不是问题,而是改变
$result = $stmt->fetchall();
到
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
因为您使用列名来获取值。