<?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 "冒称";
?>
$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);
?>
$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);
?>