在我的示例中,我有一个PHP脚本,来自AJAX,记录了每个名为'Richard'的人。我想通过计算名为Richard的人来进一步修改这个,这些人匹配某个输入值的年龄,如$theirage
所示。
具体来说,我想总结两个时代,并且只显示我的数组中等于20的那些年龄。 $row[age]
对应于我的数据库中的值age
,而$theirage
是用户输入。
最后,我想仅计算等于20的总和年龄,在这种情况下。
$theirage = $_GET['theirage'];
$query = mysqli_query($con, "SELECT count(*) as cnt FROM members WHERE
name = 'Richard'
");
$row = mysqli_fetch_assoc($query);
$bow = $row['age'] + $theirage;
$if ($bow = 20){
$person = $row['cnt'];}
echo json_encode( array(
'person' => $person
) );
我怎么能这样做?
答案 0 :(得分:1)
我建议您查询members
表,以计算符合条件的所有人:具体名称'Richard'
和特定年龄$_GET['theirage']
:
<?php
$theirAge = $_GET['theirage'];
// This is important: since we're using $theirAge in query string, we need to escape it
$theirAge = mysqli_escape_string($theirAge);
// Let's query all members whose name is Richard and age is equal to $_GET['theirage']
$query = mysqli_query($con, "SELECT count(*) FROM members WHERE name = 'Richard' AND age + $theirAge = 20");
$row = $query->fetch_row();
$totalRichardsOfGivenAge = $row[0]; // here is your result.
// And now let's return JSON
echo json_encode($totalRichardsOfGivenAge)