以下代码工作正常,它从文件夹中随机选择一个图像,并将其指定为生成的Div的背景。这有效。如果我第二次使用这个代码,但是用它来简单地放置一个随机图像的img-src,这也有效。但是,以与第一个相同的方式使用代码会破坏网站。我做错了什么?
<!-- BEGIN FEATURED 1 -->
<?php
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch (PDOException $e) {
echo $e->getMessage();
echo 'Could not establish a connection to the database.';
}
$query = $conn->prepare('SELECT `articleid`,`title` FROM `news_articles` WHERE featured = 1 ORDER BY RAND() LIMIT 1');
$array = array(
'N'
);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_COLUMN, 0);
foreach ($results as $row) {
}
$image = get_rand_img('images/featured_images/csgo/');
$title = $result['title'];
echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">';?>
<div id="featured-image" style="height: 267px; width: 292px; background:url(/images/featured_images/csgo/<?php echo $image ?>)">
<?php
$result = $conn->prepare("SELECT `articleid`,`title`,`short_title` FROM `news_articles` WHERE articleid=$row");
$result->execute();
$rows = $result->fetch();
echo '<div class="featuredtitle1">';
echo $rows['short_title'];
echo '</div>';
echo '</div>';
echo '</a>';
?>
下面的代码打破了网站..(我现在已经评论过了,你可以看到)。
<!-- BEGIN FEATURED 2 (Disabled) -->
<?php
/*
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch (PDOException $e) {
echo $e->getMessage();
echo 'Could not establish a connection to the database.';
}
$query = $conn->prepare('SELECT `articleid` FROM `news_articles` WHERE featured = 1 ORDER BY RAND() LIMIT 1');
$array = array(
'N'
);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_COLUMN, 0);
foreach ($results as $row) {
}
$image = get_rand_img('images/featured_images/dota/');
$title = $result['title'];
echo '<a href="index.php?viewarticle=1&articleid=' . $row . '">';?>
<div id="featured-image2" style="height: 267px; width: 292px; background:url(/images/featured_images/dota/<?php echo $image ?>)">
<?php
$result = $conn->prepare("SELECT `articleid`,`title`,`short_title` FROM `news_articles` WHERE articleid=$row");
$result->execute();
$rows = $result->fetch();
echo '<div class="featuredtitle2">';
echo $rows['short_title'];
echo '</div>';
echo '</div>';
echo '</a>';
*/
?>
我不明白为什么第一段代码没问题,但重复它会再次破坏它?
答案 0 :(得分:0)
在编辑评论之前,我看到了一个关于以数组形式访问对象的致命错误。尝试更改您的代码:
$results = $query->fetchAll(PDO::FETCH_COLUMN, 0);
对此:
$results = $query->fetchAll(PDO::FETCH_ASSOC);