日期:2014-05-16  浏览次数:20800 次

《PHP---mysql数据连接简单示例》
<?php
/*
create table user1(
id int primary key auto_increment,
name varchar(32) not null,
password varchar(64) not null,
email varchar(32) not null,
age tinyint unsigned not null
);

insert into user1(name,password,email,age)values('wxf',md5('123'),'wxf@sina.com',30);
insert into user1(name,password,email,age)values('zhangsan',md5('456'),'zhangsan@sina.com',30);
insert into user1(name,password,email,age)values('lisi',md5('789'),'lisi@sina.com',30);
*/
	//1.获取连接
	$conn = mysql_connect("localhost","root","123");
	
	if (!$conn){
		die("连接失败".mysql_error());
	}else{
		echo "连接成功<br/>";;
	}
	
	//2.选择数据库
	mysql_select_db("test");
	
	//设置编码
	mysql_query("set names UTF-8");
	
	$res = mysql_query($sql, $conn);
	
	$sql = "select * from user1";
	
	//3.接受资源
	$res = mysql_query($sql, $conn);
	
	while ($row = mysql_fetch_row($res)){
		foreach ($row as $key=>$value){
			echo "---".$value;
		}
		echo "<br/>";
	}
	
	//4.释放资源必须有
	mysql_free_result($res);
	//关闭mysql连接,写与不写效果不大
	mysql_close();
?>