0

我正在尝试用 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();

    }
4

1 回答 1

0

尝试改变

foreach($result as $sline) {

foreach($result as $line) {

在那种情况下,这不是问题,而是改变

$result = $stmt->fetchall();

$result = $stmt->fetchall(PDO::FETCH_ASSOC);

因为您使用列名来获取值。

于 2013-02-28T05:21:26.590 回答