Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中使用mysql创建一个好的get和set函数?_Php_Mysql_Get_Set - Fatal编程技术网

如何在PHP中使用mysql创建一个好的get和set函数?

如何在PHP中使用mysql创建一个好的get和set函数?,php,mysql,get,set,Php,Mysql,Get,Set,我想在PHP中创建一个函数,使用mysql获取并设置从我的browsergame到数据库的stat include ("connection.php"); function getstat($user_id) { } function setstat($user_id) { } 因此,我的数据库中有一个存储统计数据的表,因此统计id必须等于变量$user_id,所以对我来说,这非常困难,有人能帮忙吗 这里是一个带有mysql_*函数的示例(但您需要使用它来实现,mysql_*函数将被删

我想在PHP中创建一个函数,使用mysql获取并设置从我的browsergame到数据库的stat

include ("connection.php");

function getstat($user_id)
{

}

function setstat($user_id)
{

}

因此,我的数据库中有一个存储统计数据的表,因此统计id必须等于变量$user_id,所以对我来说,这非常困难,有人能帮忙吗

这里是一个带有mysql_*函数的示例(但您需要使用它来实现,mysql_*函数将被删除并将被弃用):

在第二个函数中,还需要传递第二个var和新的stat值

function setstat($user_id, $newValue)
{
 mysql_query("UPDATE user_stats SET fieldUpdated=".mysql_real_scape_string(newValue)." WHERE stat_id=".mysql_real_scape_string($user_id));
}

它其实并没有那么复杂,所以我强烈建议学习一些SQL。关于这一主题,有一些非常基础的学校课程,请查看。当然,不要停下来谈W3学校,但这是一个让你的头脑围绕这个话题的好方法

为了快速参考,这里有一个与mysqli的连接设置

$con = new mysqli('database','username','password','database');
也就是说,让我们看看一些查询

首先,我假设您的连接代码已经设置了一个mysqli连接供我们使用,我还假设您已经准备好了mysqli的api文档,并且打开了autocommit,这样我们就不会处理事务了(这是初学者真正想要避免的)

让我们插入一个stat

//首先,查看用户是否存在,在getstat($id)中

//现在,让我们实现getstat,这将非常简单

$stmt = $con->prepare('SELECT * FROM `stat_table` WHERE id=:id');
$stmt->bind_param('n',':id');
$stmt->execute();


if($stmt->num_rows == 1){//user exists and here is the data!!
    $stmt->bind_result($id,$users_name,$some_other_value);//one variable for each column returned
    $result_row = $stmt->fetch();
    printf('id: %n and user_name: %s and some_other_value: %s\n',$id,$users_name,$some_other_value);
    $stmt->close();//don't leave me hangin guys
    return $result_row;//because the user exists and we output their info! Yay!! We're also going to return the row as an object so that whoever called this function can use the data. Since this is non null a simple if($result_row) will return true.
} else { //nope, user doesn't exist (0 rows returned), so we need to inform the system
    return false;//this is why setstat has a type check in it, since we can return two types of data, one object, one boolean.
}
注意:这段代码假设执行时没有错误,并且您手头有mysqliapi文档,可以阅读它们。它还可能包含语法和使用错误,因为我写得相当快:)

说明:我们在这里所做的工作概述了SQL中一些非常常见的操作。首先,我们需要使用SELECT语句根据ID抓取一行数据。出于安全原因,我们将使用准备好的语句对象将ID中潜在的恶意代码替换为一个干净的数字,以便进行查找

然后,基于此,我们将运行UPDATE语句将stat设置为新值,或者运行INSERT语句将该行和stat实际相加。这一切都使用预先准备好的语句,如果您不熟悉SQL和数据库系统,您可能会发现这些语句有点高级,但出于安全原因,它们非常重要。签出API并在wikipedia或其他网站上阅读它们

那么,为什么书中有答案呢?坦率地说,您似乎对SQL“语言”(我在引号中加了引号,因为它在不同的DBMS中的用法略有不同)没有很好的理解,我们在这里学习。因此,我不只是为您编写代码,而是想给您一些解释,即使这已经得到了回答:)


我希望这对现在和将来的工作都有帮助

您知道SQL的基本知识吗?如何执行SELECT、INSERT和UPDATE语句?您使用什么API与数据库(connection.php中发生了什么)、MySQLi、PDO进行通信?如果没有更多信息,我们将无法帮助您。hi micheal in my connection.php是与我的数据库的连接,我知道如何使用SELECT、INSERT和UPDATE,但在这里很难使用,我必须检查$user_id是否等于我数据库中的user_id,以便我可以获取该统计数据。我希望您现在就理解它…抱歉,需要更多的细节。当然,我们知道您的连接发生在connection.php中,但在php中连接MySQL数据库有多种不兼容的方法,我们只能猜测您使用哪种方法。您尝试了什么?为您准备了一个很长的答案,standby:)希望它能解释一些事情。请编辑您的答案,说明至少mysql_*函数已被弃用,然后我将删除我的DOWNDOVITE。ty我将尝试一下
if(getstat($id) !== false){//a user was found via getstat($id), note the type check
    $stmt = $con->prepare('UPDATE `stat_table` SET=stat_user WHERE id=:id');
}
else{
    $stmt = $conf->prepare('INSERT INTO `stat_table` VALUES('','users name','some other column value');
}
$stmt->bind_param('n',':id');
$stmt->execute();
$stmt->close();
$stmt = $con->prepare('SELECT * FROM `stat_table` WHERE id=:id');
$stmt->bind_param('n',':id');
$stmt->execute();


if($stmt->num_rows == 1){//user exists and here is the data!!
    $stmt->bind_result($id,$users_name,$some_other_value);//one variable for each column returned
    $result_row = $stmt->fetch();
    printf('id: %n and user_name: %s and some_other_value: %s\n',$id,$users_name,$some_other_value);
    $stmt->close();//don't leave me hangin guys
    return $result_row;//because the user exists and we output their info! Yay!! We're also going to return the row as an object so that whoever called this function can use the data. Since this is non null a simple if($result_row) will return true.
} else { //nope, user doesn't exist (0 rows returned), so we need to inform the system
    return false;//this is why setstat has a type check in it, since we can return two types of data, one object, one boolean.
}