【总结】纯php 操作 mysql 数据库

1、连接Mysql


$con = mysql_connect("localhost","root","");
if (!$con){
	echo 'Could not connect: ' . mysql_error();
}


2、选择数据库


mysql_select_db("test", $con);


3、数据库查询


$sql = "INSERT INTO table_name (id, name, status) VALUES('123456','me',1)";

$sql = "UPDATE table_name SET `status` = 0 WHERE `id` = '123456'";

$sql = "DELETE FROM table_name WHERE `id` = '123456'";

$sql = "SELECT * FROM table_name WHERE `status` = 1";

$result = mysql_query($sql);


4、判断查询是否成功


select的时候


if(mysql_num_rows($result)>=0){
	//query success
}else{
	//query failed
}


insert、delete、update的时候


if(mysql_affected_rows($con)>0){
	//query success
}else{
	//query failed
}


5、处理select返回的数据


if((mysql_num_rows($result)>0)){
	while($row = mysql_fetch_assoc($result))
	{
		$tmp_array[] = $row;
	}
}

$temp_array中就是以数组的形式保存着返回的数据


6、关闭连接


mysql_close($con);


你可能感兴趣的:(sql,PHP,mysql,数据库,增删改查)