php 连接数据库

先是测试连接数据库:

$con = mysql_connect("localhost:3306","root","1234");//链接数据库
  if(!$con){
   die ("Could not connect: ". mysql_errno());
  }
mysql_selectdb("my_db",$con);//选择数据库

//创建表
$sql = "create table person(person_id int not null auto_increment,primary key(person_id),
    first_name varchar(15),
    last_name varchar(15),
    age int)";
//插入数据
$sql = "insert into person(first_name,last_name,age) values('yin','qi','21')";
//查询数据
$result = mysql_query("select * from person order by age");
while($row=mysql_fetch_array($result)){//使用mysql_fetch_array()函数迭代
    $row['first_name'];//使用这个访问数据库记录
}
//更新数据
$updater="update person set age ='36' where first_name='zhang' and last_name='san'";
//删除数据
$deleter = delete from person where first_name='zhang';
mysql_close($con);//关闭数据库

你可能感兴趣的:(PHP,mysql)