2013-02-28 63 views
0

我正在嘗試使用PDO中沒有運氣的數據庫中的數據填充表格。我是PHP的新手,所以我有搜索,但仍然找到解決方案。該表根本沒有填充,沒有數據。這是我的代碼。使用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>"; 

這是我如何連接到我的DB

 $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

是什麼問題? – DevT 2013-02-28 05:12:26

+0

表格根本沒有填充。 – emurray 2013-02-28 05:13:33

回答

0

嘗試改變

foreach($result as $sline) { 

foreach($result as $line) { 

這不是在這種情況下的問題,但改變

$result = $stmt->fetchall(); 

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

,因爲你使用的列名來獲取值。

+0

我試過但沒有工作。 – emurray 2013-02-28 05:24:03

+0

在'$ result = $ stmt-> fetchall();'之後執行'var_dump($ result);'並確保您的結果集已填充。 – peterm 2013-02-28 05:28:55

+0

您還需要更改'$ id = $ sline [「idactividades」];'請嘗試首先調試,因爲您的問題非常明顯,您沒有使用具有該數據的變量 – 2013-02-28 05:31:04