【发布时间】:2016-11-03 15:13:42
【问题描述】:
我正在从数据库中提取数据,然后将其显示在我的 html 页面上。我曾认为我下面的代码可以工作,并且它可以获取数据并将其放入变量中。尝试将变量注入 html 标记时,它会崩溃。我还可能错误地假设通过将我的 html 标签放在 php foreach 循环中,它将根据返回的行数动态创建所需的所有标签。我需要 foreach,因为数据是一个数组,所以要获取每条记录,我需要查看数组。
我将这段代码放在我想要放置元素的 body 标记中。
body 标签中的 PHP 函数位于元素所在的部分。
<?php
function db_connect() {
// Define connection as a static variable, to avoid connecting more than once
static $connection;
// Try and connect to the database, if a connection has not been established yet
if(!isset($connection)) {
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('\assets\con_config.ini');
$connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
}
// If connection was not successful, handle the error
if($connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return mysqli_connect_error();
}
return $connection;
}
function db_query($query) {
// Connect to the database
$connection = db_connect();
// Query the database
$result = mysqli_query($connection,$query);
return $result;
}
function db_select($query) {
$rows = array();
$result = db_query($query);
// If query failed, return `false`
if($result === false) {
return false;
}
// If query was successful, retrieve all the rows into an array
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
$rows = db_select("select CONCAT_WS(' ', fname, mname, lname) as author_name, title, image_location, rating, review, (Select mid(date_reviewed,1,2) from reviews where reviews.book_id = books.id) as day, (Select mid(date_reviewed,4,3) from reviews where reviews.book_id = books.id) as month, (Select mid(date_reviewed,8,2) from reviews where reviews.book_id = books.id) as year, sellers_site, twitter_site, fb_site, twitter_id, fb_id, genre from authors, books, book_genre, book_link, reviews, social_media where books.author_id = authors.id and book_genre.book_id = books.id and book_link.book_id = books.id and reviews.book_id = books.id and social_media.author_id = authors.id group by ireviews.reviews.date_reviewed ASC");
if($rows === false) {
$error = db_error();
// Handle error - inform administrator, log to file, show error page, etc.
}
//foreach($rows as $value){
//echo $value['author_name'] . "<br />\n";
//echo $value['title'] . "<br />\n";
//echo $value['rating'] . "<br />\n";
//}
?>
<? php foreach($rows as $value); ?>
用于显示返回数据的 HTML 部分。
<? php foreach($rows as $value); ?>
<div class="block">
<div class="row">
<div class="col-md-4 col-md-8">
<div class="widget-block">
<input id="rate1" value="<?php echo $value['rating']?>" type="number" class="rating" data-max="5" data-min="0" data-size="sm" data-show-clear="false" readOnly="readOnly">
<a href="<?php echo $value['sellers_site']?>" target="#"><img class="img-responsive wow fadeInLeftBig animated" data-wow-duration="1.5s" src="<?php echo $value['$image_location']?>" alt="<?php echo $value['$author_name']?>"></a>
<br>
<a href="<?php echo $value['sellers_site']?>" class="btn btn-success" target="_blank">Buy this book</a>
</div>
</div>
<div class="col-md-6 col-md-8">
<div class="section-sub-title">
<article class="section-title-body white">
<h1 class="head-title">Author: <span><?php echo $value['$author_name']?> -</span> <?php echo $value['$title']?></h1>
<span class="point-line hidden-xs hidden-sm"></span>
<p>
<?php echo $value['$review']?>
</p>
</article>
</div>
</div>
</div>
</div>
<?php } ?>
谢谢!
【问题讨论】:
-
<? php告诉我们,在你的真实代码中实际上读作<?php。 -
使用
<?php foreach($rows as $value) { ?> -
猜猜这是什么
foreach($rows as $value); -
我想没有人(下)想“解释”这个
foreach($rows as $value);中的分号,或者知道它的真正作用,也不会为它抛出错误。 “有”一个原因。 -
一直在寻求扩展我的理解 Fred -ii- :-)