我试图将MySQL数据库信息显示为HTML。我已经在这个论坛上研究了20多个主题,尝试使用他们的解决方案,但我仍然遇到问题。正在执行以下代码。我试图从数据库中读取courseNumber,section,instructor,startTime,endTime,days和name。我认为foreach将显示查询中的所有数据,但没有显示任何内容,只有我的表头和底部的用户输入按钮。我确保填充了数据库。我试图使用while循环和来自在线解决方案的不同foreach循环,但没有运气。如果不是很明显,我是一名编程学生,并且正在努力掌握这一主题。非常感谢所有的时间和精力! 这是我上传到http://einstein.etsu.edu/~latture/hw4/main.php
的网站//this is the function I'm using to call my database. This part is working correctly(executed below)
static function readAll()
{
$pdo = Database::connect();
$sql = "Select * from course";
$data = $pdo->prepare($sql);
$data->execute();
$data->fetchAll();
Database::disconnect();
return $data;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title></title>
</head>
<body>
<div class="row">
<div class="col">
<table class="table">
<thead>
<tr>
<th>Course Number</th>
<th>Section</th>
<th>Instructor</th>
<th>Start Time</th>
<th>End Time</th>
<th>Days</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
include_once "courseController.php";
include_once "schedule.php";
include_once "student.php";
$courses = courseController::readAll();
foreach($courses as $courseDB) {
echo '<tr>' .
'<td>' . $courseDB['courseNumber'] . '</td>' .
'<td>' . $courseDB['section'] . '</td>' .
'<td>' . $courseDB['startTime'] . '</td>' .
'<td>' . $courseDB['endTime'] . '</td>' .
'<td>' . $courseDB['instructor'] . '</td>' .
'<td>' . $courseDB['days'] . '</td>' .
'<td>' . $courseDB['name'] . '</td>' .
'</tr>';
}
?>
</tbody>
</table>
</div>
</div>
<div class="row">
<form = method ="post" action = "process.php">
<div class = "col">
<div class="form-group">
<select class="form-control" name="ActionType">
<option id="create" value="create">Create!</option>
<option id="update" value="update">Update..</option>
<option id="delete" value="delete">Delete :(</option>
</select>
</div>
</div>
<div class = "col">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</body>
</html>
答案 0 :(得分:3)
您必须返回从$data->execute();
收到的结果。将readAll()
更改为
static function readAll()
{
$records = array();
$pdo = Database::connect();
$sql = "Select * from course";
$data = $pdo->prepare($sql);
$data->execute();
$records = $data->fetchAll();
Database::disconnect();
return $records;
}
答案 1 :(得分:0)
你应该使用这个
$data= $pdo->query($sql);
$result = $query->rowCount();
if($result > 0){
$r = $result->fetchAll();
}else{
$r = 0;
}
return $r;