php操作mysql示例备忘录

本文介绍了使用SQL进行数据库操作的方法,包括插入、更新、查询等常见操作,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 一般的insert

$query = "INSERT INTO Profile (userName) VALUES ('{$userName}')";
$this->db->query($query);
  	 
$userId = sprintf("%d", $this->db->insert_id);
$this->db->commit();

2. 数据库中如果有,则更新,没有则插入

$query = "INSERT INTO BookRead (userId, bookId, count) VALUES ($_userId, $_bookId, 1) ON DUPLICATE KEY UPDATE count = count + 1";
$this->db->query($query);
$this->db->commit();

3. 更新

$query = "UPDATE Profile Set deviceToken='{$_token}' WHERE userId=$_userId";
$this->db->query($query);
$this->db->commit();

4.查询1,操作需要操作的字段

$stmt = $this->db->prepare('SELECT userId, passWord FROM Profile WHERE userName=?');
$stmt->bind_param("s", $_userName);
$rs = $stmt->execute();
$stmt->bind_result($_userId, $_passWord);
while ($stmt->fetch()) {
	break;
}
$stmt->close();


查询2,返回查询结果数组

$query = "SELECT name, points FROM Profile WHERE 1 ORDER BY points DESC LIMIT $_from, $_to";
			
if ($result = $this->db->query($query)) {
		
	
	while ($row = $result->fetch_row()) {
					
		$ret = array (
				"name" => $row[0],
				"points" => $row[1],
		);
	}
	$result->close();
		
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值