函数的作用是:运行查询3次

时间:2022-02-26 13:18:03

My problem is that: I have a users table in MySQL. I made a query with MySQLi which looks like:

我的问题是:我有一个MySQL的用户表。我用MySQLi做了一个查询

if($stmt = $mysqli->prepare("SELECT condition,name,money FROM users WHERE fbid = ?")){

 $stmt->bind_param('s',$_SESSION['FBID']);

 $stmt->execute();

 $stmt->store_result();

 $num_of_rows = $stmt->num_rows;

 $stmt->bind_result($condition,$name,$money);

 while ($stmt->fetch()) {

And here's my problem, because I want different users to add equal usernames to their ids. So, my code is the following:

这是我的问题,因为我希望不同的用户在他们的id中添加相同的用户名。因此,我的代码如下:

if($_GET['name']!='' && $money>'500'){
$stmt2 = $mysqli->prepare("UPDATE users SET `condition` = `condition` + 5, `money` = `money` - 5 WHERE fbid = ? AND name = ?");
 $stmt2->bind_param("ss", $_SESSION['FBID'],$_GET['name']);
 $stmt2->execute();
 $stmt2->close();

I want to update only that value in the database where user ID = $_SESSION[fbid] and name = $_GET[name]. So if I have an account with id 1922838445 and I have three names, for example, John, Joe, and Jill and $_GET[name]=='Joe' then update only that value at the same ids. It works until that point that update only the got value, but it does that 3 times... Because of while () maybe??? How can I fix it?

我只想更新数据库中的那个值,其中用户ID = $_SESSION[fbid]和name = $_GET[name]。因此,如果我有一个id为1922838445的帐户,并且我有三个名称,例如,John、Joe、Jill和$_GET[name]= 'Joe',那么只更新相同id的值。它一直工作到那个点,只更新已获得的值,但它做了3次……因为while () ??我怎样才能修好它呢?

The two code samples have to come one after!! Because of checking the value of money..

这两个代码示例必须紧随其后!因为检查了钱的价值。

2 个解决方案

#1


3  

There's no need for the first SELECT and the loop, just put the condition on money into the UPDATE query itself.

无需进行第一次选择和循环,只需将金钱条件放入更新查询本身。

if ($_GET['name'] != '') {
    $stmt = $mysqli->prepare("
        UPDATE users 
        SET condition = condition + 5, money = money - 5
        WHERE fbid = ? AND name = ?
        AND money > 500")
    $stmt->bind_param("ss", $_SESSION['FBID'], $_GET['name']);
    $stmt->execute();
}

#2


1  

Try this instead to get only one record:

试试这个,只得到一个记录:

It gets only the record you need because it has a WHERE clause on both parameters that you plan to update, instead of only a partial match on the fbid like you had before.

它只获取您需要的记录,因为它在您计划更新的两个参数上都有一个WHERE子句,而不是像以前那样在fbid上只有一个局部匹配。

Before you were getting 3 records because you had a partial key search, then looping through the records and updating the same record over and over against, regardless of the value of the second part of the key in the record you were looping against.

在得到3条记录之前,因为进行了部分键搜索,然后循环遍历记录,并对同一条记录进行反复更新,而不考虑要循环使用的记录中键的第二部分的值。

if($stmt = $mysqli->prepare("SELECT condition,name,money FROM users WHERE fbid = ? AND name = ?")){

 $stmt->bind_param("ss", $_SESSION['FBID'],$_GET['name']);

 $stmt->execute();

#1


3  

There's no need for the first SELECT and the loop, just put the condition on money into the UPDATE query itself.

无需进行第一次选择和循环,只需将金钱条件放入更新查询本身。

if ($_GET['name'] != '') {
    $stmt = $mysqli->prepare("
        UPDATE users 
        SET condition = condition + 5, money = money - 5
        WHERE fbid = ? AND name = ?
        AND money > 500")
    $stmt->bind_param("ss", $_SESSION['FBID'], $_GET['name']);
    $stmt->execute();
}

#2


1  

Try this instead to get only one record:

试试这个,只得到一个记录:

It gets only the record you need because it has a WHERE clause on both parameters that you plan to update, instead of only a partial match on the fbid like you had before.

它只获取您需要的记录,因为它在您计划更新的两个参数上都有一个WHERE子句,而不是像以前那样在fbid上只有一个局部匹配。

Before you were getting 3 records because you had a partial key search, then looping through the records and updating the same record over and over against, regardless of the value of the second part of the key in the record you were looping against.

在得到3条记录之前,因为进行了部分键搜索,然后循环遍历记录,并对同一条记录进行反复更新,而不考虑要循环使用的记录中键的第二部分的值。

if($stmt = $mysqli->prepare("SELECT condition,name,money FROM users WHERE fbid = ? AND name = ?")){

 $stmt->bind_param("ss", $_SESSION['FBID'],$_GET['name']);

 $stmt->execute();