方法一:
connect.php
* 连接选择数据库 */
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect");
print "Connected successfully";
mysql_select_db("my_database") or die("Could not select database");
/* 执行 SQL 查询 */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed");
/* 在 HTML 中打印结果 */
print "
\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "\t
\n";
foreach ($line as $col_value) {
print "\t\t
$col_value | \n";
}
print "\t
\n"; }
print "
\n";
/* 释放资源 */
mysql_free_result($result);
/* 断开连接 */
mysql_close($link);
?>
方法二:
SQL建表语句:
create database phptest;
create table TEST
(
ID int auto_increment primary key,
Name varchar(32),
Age int,
Salary int
);
insert into TEST values (0,'Billy Bob Barker','87','93000');
insert into TEST values (0,'Sniffling Sam Sloth','23','12000');
insert into TEST values (0,'Roy Bartley','31','87000');
insert into TEST values (0,'Leroy Longrad','45','63000');
insert into TEST values (0,'Amy Antwerp','37','34000');
insert into TEST values (0,'Kim Kruger','57','76000');
PHP查询语句:
$mysql_server_name="localhost"; //数据库服务器名称
$mysql_username="root"; // 连接数据库用户名
$mysql_password="root"; // 连接数据库密码
$mysql_database="phptest"; // 数据库的名字
// 连接到数据库
$conn=mysql_connect($mysql_server_name, $mysql_username,$mysql_password);
// 从表中提取信息的sql语句
$strsql="select * from TEST";
// 执行sql查询
$result=mysql_db_query($mysql_database, $strsql, $conn);
// 获取查询结果
$row=mysql_fetch_row($result);
//打印结果
echo '';
echo '
';
// 显示字段名称
echo "\n
\n";for ($i=0; $i
{
echo '
'. mysql_field_name($result, $i); echo " | \n";
}
echo "
\n";
// 定位到第一条记录
mysql_data_seek($result, 0);
// 循环取出记录
while ($row=mysql_fetch_row($result))
{
echo "
\n"; for ($i=0; $i
{
echo '
'; echo "$row[$i]"; echo ' | ';
}
echo "
\n";
}
echo "
\n";
echo "";
// 释放资源
mysql_free_result($result);
// 关闭连接
mysql_close();
?>
阅读(770) | 评论(0) | 转发(0) |