if(!$conn){
echo "connect failed";
exit;
} $sql='use test';
mysql_query($sql,$conn); //增加
$sql="insert into mytest values(null,'pu','20')";
$rs=mysql_query($sql,$conn);
if($rs){
echo 'insert data success'."<br />";
}else{
echo 'insert data failed'."<br />";
} //修改
$sql="update mytest set longtitude='fu' where id=3";
$rs=mysql_query($sql,$conn);
if($rs){
echo 'update data success'."<br />";
}else{
echo 'update data failed'."<br />";
} //删除
$sql="delete from mytest where id='2'";
$rs=mysql_query($sql,$conn);
if($rs){
echo 'del data success'."<br />";
}else {
echo 'del data failed'."<br />";
} //查询
$sql="select * from mytest";
$rs=mysql_query($sql,$conn);
//打印变量的相关信息
var_dump($rs);
echo "<br/>";
//遍历从结果集中取行 mysql_fetch_array/assoc/row/object
while($row=mysql_fetch_object($rs)){
print_r($row);
echo '   ';
echo $row->id;
echo "<br />";
}
?>

effect

PHP数据库操作

php连接mysql的基本操作

header('Content-Type: text/html; charset=utf-8');
$host = '127.0.0.1';
$port = '3306';
$user = 'root';
$pass = 'root';
$charset = 'utf8';
$link = mysql_connect("$host:$port", $user, $pass);
if(!$link)
{
die('连接失败');
}

相关文章