以下代码时php在链接数据库,利用mysql操作的四大功能:增删改查,源码如下:
//连接数据库
$conn=mysql_connect('localhost','root','');
if(!$conn){
echo "connect failed";
exit;
}
$sql='use student';
mysql_query($sql,$conn);
//增加
$sql="insert into student(sname,sage) values('wx','18')";
$rs=mysql_query($sql,$conn);
if($rs){
echo 'insert data success'."
";
}else{
echo 'insert data failed'."
";
}
//删除
$sql="delete from student where sname='li'";
$rs=mysql_query($sql,$conn);
if($rs){
echo 'del data success'."
";
}else {
echo 'del data failed'."
";
}
//修改
$sql="update student set sname='fu' where sname='pu'";
$rs=mysql_query($sql,$conn);
if($rs){
echo 'update data success'."
";
}else{
echo 'update data failed'."
";
}
//查询
$sql="select * from student";
$rs=mysql_query($sql,$conn);
var_dump($rs);
while($row=mysql_fetch_object($rs)){
print_r($row);
echo ' ';
echo $row->sname ."
">;
}
?>