现在,我正在准备"gallery_kategori"应该是Gaul类别的ID
错误出现并描述如下:错误2:命令不同步;你现在不能运行这个命令
我想要从中得到什么:-希望它进入画廊,拍摄一张属于该类别或区域的照片。-它不应该一直在一起,每个图像一次只能有一个图像
function galleryKategori()
{
if ($st = $this->mysqli->prepare('SELECT id, title FROM gallery_kategori')) {
$st->execute();
$st->bind_result($id, $title);
while ($st->fetch()) {
?>
<a href="/galleri-indhold/<?php echo $id;?>/">
<div class="galleryKategori">
<h4><?php echo $title;?></h4>
<?php
if ($stmt = $this->mysqli->prepare('SELECT rank, img FROM gallery WHERE kategori = ?')) {
$stmt->bind_param('i', $kategori);
$kategori = $id;
$stmt->execute();
$stmt->bind_result($rank, $img);
while ($stmt->fetch()) {
if($rank == 2)
{
}
}
$stmt->close();
} else {
echo 'Error 2: ' . $this->mysqli->error;
}
?>
</div>
</a>
<?php
}
$st->close();
}
else {
echo 'Error 1: ' . $this->mysqli->error;
}
}
EIDT版本
function galleryKategori()
{
$st->free_result();
if ($st = $this->mysqli->prepare('SELECT id, title FROM gallery_kategori')) {
$st->execute();
$st->bind_result($id, $title);
while ($result->fetch()) {
?>
<a href="/galleri-indhold/<?php echo $id;?>/">
<div class="galleryKategori">
<h4><?php echo $title;?></h4>
<?php
if ($stmt = $this->mysqli->prepare('SELECT rank, img FROM gallery WHERE kategori = ?')) {
$kategori = $id;
$stmt->bind_param('i', $kategori);
$stmt->execute();
$stmt->bind_result($rank, $img);
while ($stmt->fetch()) {
if($rank == 2)
{
echo "Hey";
}
}
$stmt->close();
} else {
echo 'Error 2: ' . $this->mysqli->error;
}
?>
</div>
</a>
<?php
}
$st->close();
}
else {
echo 'Error 1: ' . $this->mysqli->error;
}
}
在对代码进行了一些混乱之后,这是因为您还没有释放该连接的第一个结果。
您需要使用两个MySQLi对象,或者以能够调用$st->free_result();
的方式重写代码
$stmt = $this->mysqli->prepare('SELECT rank, img FROM gallery WHERE kategori = ?')
这就是我让它正常工作所需要的一切。
编辑08/14/13:
既然你问了,我会删除这个函数(除非这是一个对象,它看起来不像它),然后混合使用内联和块php。我对你将要对代码做什么做了一些随意的处理,但这里有一个可能有效的小例子。我将结果存储在一个数组中,释放结果,然后重用同一个语句对象。
<?php
if( ( $st = $mysqli->prepare( 'SELECT id, title FROM gallery_kategori' ) ) !== FALSE ) {
$st->execute();
$st->bind_result( $id, $title );
while( $st->fetch() ) {
$categories[$id] = $title;
}
$st->free_result();
unset( $id, $title );
}
else {
echo 'Error 1: ' . $mysqli->error;
}
?>
<?php if( count( $categories ) > 0 ): ?>
<?php foreach( $categories as $id => $title ): ?>
<div class="galleryKategori">
<h4>
<a href="/galleri-indhold/<?php echo $id; ?>/">
<?php echo $title; ?>
</a>
</h4>
<?php
if( ( $st = $mysqli->prepare( 'SELECT rank, img FROM gallery WHERE kategori = ?' ) ) !== FALSE ) {
$st->bind_param( 'i', $id );
$st->execute();
$st->bind_result( $rank, $img );
while( $st->fetch() ) {
$gallery[$rank] = $img;
}
$st->free_result();
unset( $rank, $img );
}
else {
echo 'Error 2: ' . $mysqli->error;
}
?>
<?php if( count( $gallery ) > 0 ): ?>
<ul>
<?php foreach( $gallery as $rank => $img ): ?>
<li class="gallery rank-<?php echo $rank; ?>">
<a href="/galleri-indhold/<?php echo $id; ?>/">
<img src="<?php echo $img; ?>" alt="Image <?php echo $rank; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<ul>
<li>There are no images in this category.</li>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>Category Does Not Exist.</p>
<?php endif; ?>
<?php $mysqli->close(); ?>
票据
- 重新构造html元素时,内联元素(锚点)不应包含块元素(div)。我更新了上面的代码以适当地显示它