2013-06-28 60 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>"; 

}