为什么这段代码没有像我预期的那样获取查询?
Why this code does not fetch query as I expect?
如果我运行下面这个sql
SELECT date, SUM(nmbr_of_guests) FROM guest_tbl WHERE date = "2015-06-30"
我收到了:
这是我试图获取特定日期行 nmbr_of_guests
的 NUMBER(SUM) OF TOTAL VALUE。
我现在要做的是,当通过表单动态输入日期时,我想获得相同的结果(如屏幕截图所示)。
创建表单后,我将其作为表单 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);
}
}
现在,如果我 select 通过表单搜索 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
的总和)?
正如错误所说,fetch_assoc
returns 一个数组,所以 print_r($total_quests->getch_assoc())
并查看数组的结构。您可能必须选择第一个结果,但我建议您使用 foreach
循环来完成。
如果我运行下面这个sql
SELECT date, SUM(nmbr_of_guests) FROM guest_tbl WHERE date = "2015-06-30"
我收到了:
这是我试图获取特定日期行 nmbr_of_guests
的 NUMBER(SUM) OF TOTAL VALUE。
我现在要做的是,当通过表单动态输入日期时,我想获得相同的结果(如屏幕截图所示)。
创建表单后,我将其作为表单 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);
}
}
现在,如果我 select 通过表单搜索 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
的总和)?
正如错误所说,fetch_assoc
returns 一个数组,所以 print_r($total_quests->getch_assoc())
并查看数组的结构。您可能必须选择第一个结果,但我建议您使用 foreach
循环来完成。