Php 我试图在另一份准备好的声明中包含一份准备好的声明
在这个给定的准备好的声明中,它得到了文章的标题。当它获得文章标题时,它还应该将该变量发送到另一个准备好的语句中,然后该语句将查找给定文章的浏览量Php 我试图在另一份准备好的声明中包含一份准备好的声明,php,html,mysql,mysqli,prepared-statement,Php,Html,Mysql,Mysqli,Prepared Statement,在这个给定的准备好的声明中,它得到了文章的标题。当它获得文章标题时,它还应该将该变量发送到另一个准备好的语句中,然后该语句将查找给定文章的浏览量 <table class="table table-colored table-centered table-inverse m-0"> <thead> <tr> <th>Title</th> <th>Category</th> <th>Writer<
<table class="table table-colored table-centered table-inverse m-0">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Writer</th>
<th>Action</th>
<th>Views</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $con -> prepare('select tblposts.id as postid,tblposts.PostTitle as title,tblposts.PostUrl as postname, tblcategory.CategoryName as category,tblwritter.Writter as writter from tblposts left join tblcategory on tblcategory.id=tblposts.CategoryId left join tblwritter on tblwritter.WritterId=tblposts.WritterId where tblposts.Is_Active=?');
$cnt=1;
$stmt -> bind_param('i', $cnt);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($postid,$title,$postname,$category,$writter);
?>
<tr>
<?php while ($stmt->fetch()){?>
<td><b><?php echo $postname;?></b></td>
<td><?php echo $category?></td>
<td><?php echo $writter?></td>
<td>
<a href="edit-post.php?pid=<?php echo $postid;?>"><i class="fa fa-pencil" style="color: #29b6f6;"></i></a>
<a href="manage-posts.php?pid=<?php echo$postid;?>&&action=del" onclick="return confirm('Do you reaaly want to delete ?')"> <i class="fa fa-trash-o" style="color: #f05050"></i></a>
</td>
<?php
$stmt = $con -> prepare('select COUNT(ip) FROM tblviews WHERE postname = ?');
$stmt -> bind_param('s', $postname);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($ip);
?>
<?php while ($stmt->fetch()){?>
<td><?php echo $ip ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
</table>
标题
类别
作家
行动
意见
此代码的预期结果是将postname发送到准备好的语句中,然后调用任何给定文章中的视图量。简短回答:您正在覆盖循环中的
$stmt
<table class="table table-colored table-centered table-inverse m-0">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Writer</th>
<th>Action</th>
<th>Views</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $con -> prepare('select tblposts.id as postid,tblposts.PostTitle as title,tblposts.PostUrl as postname, tblcategory.CategoryName as category,tblwritter.Writter as writter from tblposts left join tblcategory on tblcategory.id=tblposts.CategoryId left join tblwritter on tblwritter.WritterId=tblposts.WritterId where tblposts.Is_Active=?');
$cnt=1;
$stmt -> bind_param('i', $cnt);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($postid,$title,$postname,$category,$writter);
?>
<tr>
<?php while ($stmt->fetch()){?>
<td><b><?php echo $postname;?></b></td>
<td><?php echo $category?></td>
<td><?php echo $writter?></td>
<td>
<a href="edit-post.php?pid=<?php echo $postid;?>"><i class="fa fa-pencil" style="color: #29b6f6;"></i></a>
<a href="manage-posts.php?pid=<?php echo$postid;?>&&action=del" onclick="return confirm('Do you reaaly want to delete ?')"> <i class="fa fa-trash-o" style="color: #f05050"></i></a>
</td>
<?php
$stmt = $con -> prepare('select COUNT(ip) FROM tblviews WHERE postname = ?');
$stmt -> bind_param('s', $postname);
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($ip);
?>
<?php while ($stmt->fetch()){?>
<td><?php echo $ip ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
</table>
详细回答,并附提示:
这可以通过先编写所有php,然后最后发布html(仅将php用于迭代和变量替换)来避免(或者至少更容易检测到)
更好的方法是将db访问和业务逻辑放在一个单独的类(即模型)中,让您的视图向模型(或控制器,取决于您对MVC的解释)询问它需要的数据
我没有办法测试它,但您应该能够使用单个查询:
select
tblposts.id as postid,
tblposts.PostTitle as title,
tblposts.PostUrl as postname,
tblcategory.CategoryName as category,
tblwritter.Writter as writter,
COUNT(tblwritter.ip) as views
from tblposts
left join tblcategory on tblcategory.id=tblposts.CategoryId
left join tblwritter on tblwritter.WritterId=tblposts.WritterId
left join tblviews on tblviews.postname = tblposts.PostUrl'
where tblposts.Is_Active=?
group by tblposts.PostUrl, tblposts.id, tblposts.PostTitle, tblCategory.CategoryName, tblwritter.Writter
(注意,MySQL可以让您在分组中只命名tblposts.postrl)
这样,您的页面可以简化为以下内容:
<?php
// do your query here
?>
<table class="table table-colored table-centered table-inverse m-0">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Writer</th>
<th>Action</th>
<th>Views</th>
</tr>
</thead>
<tbody>
<?php while ($stmt->fetch()): ?>
<tr>
<td><b><?= $postname ?></b></td>
<td><?= $category ?></td>
<td><?= $writter?></td>
<td>
<a href="edit-post.php?pid=<?= $postid?>"><i class="fa fa-pencil" style="color: #29b6f6;"></i></a>
<a href="manage-posts.php?pid=<?=$postid?>&&action=del" onclick="return confirm('Do you reaaly want to delete ?')"> <i class="fa fa-trash-o" style="color: #f05050"></i></a>
</td>
<td><?= $views ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
标题
类别
作家
行动
意见
我想你得到的不是预期的结果,但你没有说你的实际结果是什么。如果您可以编辑您的问题并添加问题,这将非常有用。同时,您的代码让我问了几个问题:1)为什么绑定参数、存储结果和绑定结果?我所需要的就是prepare($query)
和execute([$values])
。更简单,更不容易出错。2) 当然,这可以在一个查询中完成。但是当我在我的手机上看到你的单行查询时,我不能很好地描绘这些表格。。。你能把它分成几行让它可读吗?哎呀,我忘了mysqli没有在execute()
中传递参数值。IMO说,有很好的理由将其转储为PDO或为其编写包装器。