[英]How to get specific ID in SQL Query
Im trying to echo the content of a specific users information. 我试图回显特定用户信息的内容。 I am currently using
我目前正在使用
$sql = "SELECT treatment_log.tech,treatment_log.comments FROM customers LEFT OUTER JOIN treatment_log ON customers.id=treatment_fk WHERE customers.id = 12";
The number 12 will only echo the content of the patient with ID# 12. How would I be able to echo the content of any users information. 数字12将仅回显ID#12的患者内容。我将如何回显任何用户信息的内容。 In other words, I would also like to echo the content of users 13,14,15 etc individually.
换句话说,我也想分别回应用户13,14,15等的内容。 What would I have to put in the place of "12" to "GET" the id of the current user being viewed.
我必须将“ 12”代替“获取”正在查看的当前用户的ID。
I can currently do this using PHP by typing: 我目前可以通过输入以下内容使用PHP进行此操作:
$_GET['customer_id']
At first, keep the customer_id
in a variable. 首先,将
customer_id
保留在变量中。 Remember, GET
should never be used with any sensitive data. 请记住,绝对不要将
GET
与任何敏感数据一起使用。 However, as you have requested, maybe you are aware of this. 但是,按照您的要求,也许您已经意识到这一点。 Do it like below:
如下所示:
$cid = $_GET['customer_id'];
Then write sql query as following using ?
然后使用
?
编写sql查询如下 sign in place of 12
: 代替
12
:
$sql = "SELECT treatment_log.tech,treatment_log.time,treatment_log.date,treatment_log.titration_parameter,treatment_log.pain,treatment_log.bdi,treatment_log.suicidality,treatment_log.comments FROM customers LEFT OUTER JOIN treatment_log ON customers.id=treatment_fk WHERE customers.id = ?";
Then do as follows: 然后执行以下操作:
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "s", $cid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
To know about each of the above lines, read the official manual for
mysqli_stmt_init()
,mysqli_stmt_prepare()
,mysqli_stmt_bind_param()
,mysqli_stmt_execute()
,mysqli_stmt_get_result()
要了解上述每一行,请阅读
mysqli_stmt_init()
,mysqli_stmt_prepare()
,mysqli_stmt_bind_param()
,mysqli_stmt_execute()
,mysqli_stmt_get_result()
的官方手册
And finally, to get the result, use something like below: 最后,要获得结果,请使用如下所示的内容:
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "id: ". $row["tech"]. " " . $row["time"]." " . $row["date"]." " . $row["titration_parameter"]." " . $row["bdi"]." " . $row["suicidality"]." " . $row["comments"]. "<br>";
}
}
else {
echo "0 results";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.