PHP Mysqli 常用方法集合

//创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

//检测连接
if ($conn->connect_error) 
{
    die("Connection failed: " . $conn->connect_error);
}

//获取数据库返回值
if($stmt=$con->prepare("SELECT username,password FROM member WHERE username=? AND password=?"))
{
	$stmt->bind_param("ss",$username,$password);
	$stmt->execute();
	$stmt->bind_result($U,$P);
	$HasData=false;
	while($stmt->fetch())
	{
		$HasData=true;
		echo "username->".$U." password->".$P."
"; } }

//关闭连接
$conn->close();

//常用语法
INSERT INTO [表名] ([字段1],[字段2]) VALUES ([值1],[值2]);
SELECT * FROM [表名] WHERE [表达式] ORDER BY [字段1],[ASC/DESC 升/降] [字段2],[ASC/DESC 升/降];
DELETE FROM [表名] WHERE [表达式];
UPDATE [表名] SET [字段1]=[值1],[字段2]=[值2] WHERE [表达式];
获取总行数:SELECT COUNT(*) FROM [表]


//中文乱码
//针对页面
header("Content-Type:text/html;charset=utf-8");
//针对数据库
mysqli->query("SET CHARACTER SET 'utf8'");//读库 
mysqli->query("SET NAMES 'utf8'");//写库 

Codeeer@20150815 Version 1.1

你可能感兴趣的:(PHP)