【问题标题】:How can i get all the values in the query?如何获取查询中的所有值?
【发布时间】:2018-10-07 14:34:31
【问题描述】:

<?php
$mysqli = new mysqli("localhost", "root", "", "titan3d");


if (mysqli_connect_error()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$sdate = "";
$stime = "";

if(isset($_POST['sdate']))
	{
		$sdate = $_POST["sdate"];
	}
if(isset($_POST['stime']))
	{
		$stime = $_POST["stime"];
	}	


$statement = $mysqli->prepare("SELECT bookedseat FROM bookings WHERE sdate = ? AND stime = ?");{


    $statement->bind_param("si", $sdate, $stime);


    if (!$statement->execute()) {
        trigger_error('Error executing MySQL query: ' . $statement->error);
    }


    $statement->bind_result($book);


    $statement->fetch();

    printf($book);
	

	//header('Location: http://localhost/My%20Project/seats.html');


    $statement->close();
}


$mysqli->close();

?>

这是我的 php 文件,用于从表单中获取数据并进行查询,然后显示结果。

执行后,php完美运行。

但它只显示查询中的第一个值。

为什么会这样?

如何显示查询中的所有值?

【问题讨论】:

标签: php html


【解决方案1】:

您需要在while循环中使用$stmt-&gt;fetch(),然后您可以遍历每个返回的行。

while ($stmt->fetch()) {
    // $book will have the value of bookedseat for the current row
}

【讨论】:

  • $seats = array(); while ($statement->fetch()) { $seats[] = $book; } print_r($seats);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多