You might want to do something like this to output the column names as well:
<?php
echo "<table border=\"1\">\n";
$line = mysql_fetch_array($result, MYSQL_ASSOC);
echo "\t<tr>\n";
echo "\t\t<th>#</th>\n";
foreach (array_keys($line) as $col_value) {
echo "\t\t<th>$col_value</th>\n";
}
echo "\t</tr>\n";
$i=0;
do {
echo "\t<tr>\n";
$i++;
echo "\t\t<th>$i</th>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}while ($line = mysql_fetch_array($result, MYSQL_ASSOC));
echo "</table>\n";
?>
(Ok, here I also added line numbers; the main point of this post is using array_keys(...) to display the column names, and using a do {...} while (...); construct to read the first line only once).
Bu basit örnek bir MySQL sunucuya nasıl bağlanılacağını, bir sorgunun nasıl çalıştırılacağını, sonuç satırlarının nasıl bastırılacağını ve bağlantının nasıl kesileceğini gösterir.
Örnek 1 - MySQL eklentisi örneği
<?php
// Bağlanıyor, veritabanı seçiliyor
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Bağlanamadı: ' . mysql_error());
echo 'Başarıyla bağlandı';
mysql_select_db('my_database') or die('Veritabanı seçilemedi');
// SQL sorgusu uygulanıyor
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Sorgu başarısız: ' . mysql_error());
// Sonuçlar HTML olarak gösteriliyor
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Sonuç kümesi serbest bırakılıyor
mysql_free_result($result);
// Bağlantı kapatılıyor
mysql_close($link);
?>
Sz.abi
19-Feb-2009 03:26
