2013-06-28 62 views
0

新的PHP和SQL,並爭取資源。PHP循環從SQL拉(初學者)

基本上我需要做的就是從表中選擇數據並將其格式化爲適當的html格式。

我有一般的想法,只是沒有設置循環。 我有SQL連接建立到數據庫,然後到表。

這裏是代碼。 http://pastebin.com/vdTJgU5z 和表格的佈局。 http://i.imgur.com/JIpfI3g.png

感謝球員,如果有什麼似乎讓我知道的話,因爲我寧願現在糾正它,而不是以後的賽道。

CODE:

$query = mysqli_query($con,"SELECT * FROM tablenamehere"); 
while(????){ 
$game = $temp['game']; 
$stance = $temp['stance']; 
$start = $temp['start']; 
} 
echo '<div id="accordion"> 
     <h3> echo $game $stance $start </td></h3> 
     </div>'; 
+1

這裏粘貼代碼,而不是在引擎收錄。 –

+0

糾錯:將代碼粘貼到;你需要答案,那些願意幫助有偏好的人 – vol7ron

+0

php手冊中的例子涵蓋了你的確切問題。你看過它嗎? –

回答

0

我們都建議使用PDO進行MySQL連接。

這裏有一個快速和骯髒方式上手(未測試):

$pdo = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass'); 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // all pdo errors will throw an exception 

try { 
    $result = $pdo->query("SELECT * FROM myTable"); 
    while($row = $result->fetch(PDO::FETCH_ASSOC)) { 
    echo "<tr>"; 
    echo " <td>" . $row['column_name'] . "</td>"; 
    echo " <td>" . $row['column_name2'] . "</td>"; 
    echo " <td>" . $row['column_name3'] . "</td>"; 
    echo "</tr>"; 
    } 
} catch(PDOException $e) { 
    echo "Something went wrong with the query. {$e->getMessage()}"; 
} 
0

試試這個

$query = mysqli_query($con,"SELECT * FROM tablenamehere"); 
while($query){ 
$game = $query['game']; 
$stance = $query['stance']; 
$start = $query['start']; 

echo "<div id='accordion'> 
     <h3><tr><td>".$game."</td><td>".$stance."</td><td>".$start."</td></tr></h3> 
     </div>"; 

}