Ben veritabanı sütun tarih ve isimde yani 'belgeli' bir tablo var. Ben tarih sütunundan adı sütununu ve MONTH_NAME ve yılı seçin ve sütun (seri numarası, adı, ay, yıl) ile ilgili bir html tablo oluşturmak istiyorum. Ben bu PHP nasıl yapabilirim?
the solution that use PDO is a good one, i personnaly prefer using it. However if you need to use the mysql functions of PHP, here is an alternative that use mysql_field_name().
Aşağıdaki Exemple de, biz sorgu seçilen alanlara için-her döngü uygulayarak tablo başlığı oluşturmak:
$db_host = "localhost";
$db_user = "myuser";
$db_pass = "mypassword";
$db_name = "mydatabase";
$connexion = mysql_connect($db_host, $db_user, $db_pass) or die (mysql_error());
$db = mysql_select_db($db_name, $connexion) or die(mysql_error());
$result = mysql_query("SELECT * FROM mytable");
?>
<table>
<thead>
<?php
for ($i=0; $i < mysql_num_fields($result); $i++) {
echo '<th>'.mysql_field_name($result, $i).'</th>';
}
?>
</thead>
<tbody>
<?php
.....
o zaman sadece sorguyu işlemek zorunda ve tabloyu doldurmak
Look into the chapter for PDO in the PHP Manual. Temelde, böyle bir şey kullanmak istiyorsanız:
$sth = $dbh->prepare("SELECT col1, col2, col3 FROM tablename");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
Daha sonra bir HTML şablonuna $ sonucunu yazınız. Eğer bir zorlama varsa associate array, bu kadar basittir:
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys($result[0])) ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row):
<tr>
<td><?php echo implode('</td><td>', $row) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Bu irade çıktı tüm tablo kafa sütunları gibi dizinin anahtarları ve daha sonra dizi üzerinde döngü ve tablo hücreleri için dizinin değerleri ile giriş başına bir satır oluşturun. Eğer daha fazla denetim istiyorsanız, bunun yerine imploding bu kadar tuşları aracılığıyla dizi erişebilirsiniz.
Sen $ sonuç dizi olsa şablonuna yazmadan önce bir şey olup olmadığını kontrol etmek isteyecektir.
Note that the above is not copy & paste ready. You'd have to figure some stuff yourself. If you don't know any function names I used, look them up. What you ask is basic stuff, so you should also be able to find plenty tutorials on Google.