如果我在sql
SELECT date, SUM(nmbr_of_guests) FROM guest_tbl WHERE date = "2015-06-30"
我得到了这个:
这就是我想要获取特定日期行nmbr_of_guests
的总值的NUMBER(SUM)。
我现在需要做的是,当通过表格动态输入日期时,我想获得相同的结果(在屏幕截图中显示)。
创建表单后,我将其设置为action
if (isset($_POST['search'])){
$serch_text = $_POST["search_text"];
$con=mysqli_connect("localhost","root","","guest");
// Check connection
if (mysqli_connect_errno()) {
die ("Failed to connect to MySQL: " . mysqli_connect_error());
}
$searching_date = $serch_text;
$searchroute = "select * from guest_tbl where date = '$searching_date'";
$result = $con->query($searchroute);
$row = $result->fetch_assoc();
if ($row == 0) { echo '<div style="color: red;">We could not find the Reference ID: <b>' . $serch_text . '</b>. Please refine your reference ID</div>' ;
} else {
echo '<h3>Total guest for the day : '. $serch_text .'</h3>';
$total_guests = mysqli_query($con, 'SELECT date, SUM(nmbr_of_guests) FROM guest_tbl WHERE date = "'.$serch_text.'"');
echo $total_guests;
mysqli_close($con);
}
}
现在,如果我选择通过表单搜索2015-06-30,我将获得Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\guest\index.php on line 26
第26行是echo $total_guests;
如果我更改echo $total_guests;
要
echo $total_guests->fetch_assoc();
我正在Notice: Array to string conversion in C:\xampp\htdocs\guest\index.php on line 25
Array
现在第25行是echo $total_guests->fetch_assoc();
如何获取特定日期的总值(nmbr_of_guests
之和)?
答案 0 :(得分:1)
正如错误所说,fetch_assoc
返回一个数组,所以print_r($total_quests->getch_assoc())
并查看数组的结构。你可能必须拿起第一个结果,但我建议你使用foreach
循环来做。