【问题标题】:I am trying to include a prepared statement within another prepared statement我正在尝试在另一个准备好的语句中包含一个准备好的语句
【发布时间】:2020-02-29 08:57:34
【问题描述】:

在这个给定的准备好的语句中,它获取文章标题。当它获得文章标题时,它还应该将该变量发送到另一个准备好的语句中,然后找到给定文章的浏览量。

<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>
    &nbsp;<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 发送到准备好的语句中,然后调用任何给定文章中的浏览量。

【问题讨论】:

  • 我认为你得到的结果不是预期的结果,但你没有说你的实际结果是什么。如果您可以编辑您的问题并添加一个问题,那将会很有帮助。同时,您的代码让我问了几个问题:1)为什么要绑定参数、存储结果和绑定结果?我只需要prepare($query)execute([$values])。更简单,更不容易出错。 2)当然这可以在一个查询中完成。但是当我在手机上查看您的单行查询时,我无法完全描绘这些表格……您能否将其分成单独的行以使其可读?
  • 糟糕,我忘记了 mysqli 没有在 execute() 中传递参数值。 IMO,有充分的理由转储它以支持 PDO 或为其编写包装器。

标签: php html mysql mysqli prepared-statement


【解决方案1】:

简答:你在循环中覆盖了$stmt

长答案,提示: 这可以通过首先编写所有 php,然后最后发出 html(仅使用 php 进行迭代和变量替换)来避免(或至少更容易检测到)。

更好的方法是将数据库访问和业务逻辑放入单独的类(即模型)中,并让您的视图向模型(或控制器,取决于您对 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 可能让您只在 group by 中命名 tblposts.PostUrl 就可以逃脱)

这样,您的页面可以简化为:

<?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>
&nbsp;<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多