<?php
$user = "root";
$passwd = "root";
$connect = mysql_connect('localhost', $user, $passwd)
or die('資料庫連接失敗: ' . mysql_error());
echo '<BR><b>第一步:</b>成功建立串連! <BR>';
$db = 'test';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo '<b>第二步:</b> 成功串連到 ('.$db.')!<BR>';
mysql_close($connect);
echo "冒稱";
?>
擷取資料庫裡面的資訊
<?php
$user = "root";
$passwd = "root";
$connect = mysql_connect('localhost', 'root', 'root')
or die('Could not connect: ' . mysql_error());
mysql_query("SET NAMES 'GB2312'",$connect);
echo '<b>第一步:</b>成功建立串連! <BR>';
$db = 'test';
mysql_select_db($db) or die('Could not select database ('.$db.') because of : '.mysql_error());
echo '<b>第二步:</b> 成功串連到 ('.$db.') !<BR>';
$query = 'SELECT * FROM user';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "<br><br><b>擷取資料...</b><br>";
echo "<table border='1' width='3000'>\n";
$count = 0;
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";
}
$count++;
echo "\t</tr>\n";
}
echo "</table>\n";
if ($count < 1) {
echo "<br><br>表格中未發現記錄.<br><br>";
} else {
echo "<br><br>表格中有".$count."行記錄.<br><br>";
}
mysql_free_result($result);
mysql_close($connect);
?>