我是PHP的新手,我坚持使用最后一段代码。它出现了以下错误,我不知道如何解决它。
PDOStatement类的对象无法转换为字符串 第70行/home/e0753906/public_html/test4.php
一旦从下拉列表中选择了所选日期,我就会尝试将数据库中的数据显示在表格中。下拉列表中选择的日期必须是WHERE子句中的日期。
我的php文件如下所示:
<?php
// set up database global variables
$servername = "localhost";
$username = "e0753906_Sam";
$password = "Tamara1623";
//connect to the database
try {
$conn = new PDO("mysql:host=" . $GLOBALS['servername'] .
";dbname=e0753906_OaktownFootball", $GLOBALS['username'],
$GLOBALS['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "An error occured: " . $e->getMessage();
}
// global variables
$query = $conn->query('SELECT DISTINCT `Date` FROM Fixtures');
$data = $conn->query('SELECT * FROM Fixtures');
$test = $conn->query('SELECT * FROM Fixtures WHERE `Date`=20160709');
$data_s = $conn->query('SELECT Date,
(SELECT Team_Name FROM Teams WHERE Teams.Team_ID =
Home_team.Team_ID) As Home_Team,
(SELECT Team_Name FROM Teams WHERE Teams.Team_ID =
Away_team.Team_ID) As Away_Team,
Home_Score,
Away_Score
FROM Fixtures
JOIN Home_team ON Home_team.Home_Team_ID =
Fixtures.Home_Team_ID
JOIN Away_team ON Away_team.Away_Team_ID =
Fixtures.Away_Team_ID');
?>
<html>
<head>
<title>Oaktown Football Club</title>
</head>
<body>
<form action="test2.php" method="get">
<select name="Date">
<option>Choose a date</option>
<?php foreach ($query->fetchAll() as $Fixtures):?>
<option name="Date" value="<?php echo $Fixtures['Date'];?>">
<?php echo $Fixtures['Date'];?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Show details">
</form>
<?php
echo "<table border=1>
<tr>
<th>Date</th>
<th>Home Team</th>
<th>Away Team</th>
<th>Score</th>
</tr>" ;
if (isset($_GET['Date'])) {
ob_start();
echo $_GET['Date'];
$datetest = ob_get_contents();
$fixture = "
{$data_s}
WHERE `Date` = ' . $datatest . '";
while($records = $fixture->fetch()) {
echo "<tr>";
echo "<td>" . $records['Date'] . "</td>";
echo "<td>" . $records['Home_Team'] . "</td>";
echo "<td>" . $records['Away_Team'] . "</td>";
echo "<td>" . $records['Home_Score'] . "-" .
$records['Away_Score'] . "</td>";
echo "</tr>";}
}
echo "</table>" ?>
</body>
由于
我也试过
//Get data from date
if (isset($_GET['Date'])) {
$dateQuery = "
{$data}
WHERE `Date` = :date";
$date = $conn->prepare($dateQuery);
$date->execute(['date'=>$_GET['Date']]);
$selectedDate = $date->fetch(POD::FETCH_ASSOC);
var_dump($selectedDate);};
答案 0 :(得分:0)
设置$data_s
时,这是执行查询的结果,所以在行中..
$fixture = "
{$data_s}
WHERE `Date` = ' . $datatest . '";
您尝试将执行查询的结果添加到字符串中。我认为您只是尝试在此声明的末尾添加WHERE
子句,我认为您只需将$data_s
设置为...
$data_s = 'SELECT Date,
(SELECT Team_Name FROM Teams WHERE Teams.Team_ID =
Home_team.Team_ID) As Home_Team,
(SELECT Team_Name FROM Teams WHERE Teams.Team_ID =
Away_team.Team_ID) As Away_Team,
Home_Score,
Away_Score
FROM Fixtures
JOIN Home_team ON Home_team.Home_Team_ID =
Fixtures.Home_Team_ID
JOIN Away_team ON Away_team.Away_Team_ID =
Fixtures.Away_Team_ID';
但我也不确定......
ob_start();
echo $_GET['Date'];
$datetest = ob_get_contents();
你可以删除它,只需使用...
$fixture = "
{$data_s}
WHERE `Date` = ' . $_GET['Date'] . '";
<强>更新强> 由于现在不执行查询,您需要在此处执行...
$fixture = $conn->query("{$data_s}
WHERE `Date` = '".$_GET['Date']}."'");
if ( $fixture === false ) {
die ($conn->error);
}