【问题标题】:Fatal error: Uncaught Error: Cannot use object of type mysqli_result as array. How do i print an attribute of my table? [duplicate]致命错误:未捕获错误:无法使用 mysqli_result 类型的对象作为数组。如何打印表格的属性? [复制]
【发布时间】:2019-02-26 21:45:53
【问题描述】:

我正在尝试打印查询结果,但出现此错误:

致命错误:未捕获错误:无法使用 mysqli_result 类型的对象作为数组

我是 PHP7 的新手,一直使用 PHP5。我尝试了不同的方法来打印结果。但我仍然得到同样的错误。

$conn=mysqli_connect("localhost","root","","vivereingegneria")  OR 
DIE(mysqli_error());
     //QUERY
    $str= "SELECT `Titolo`,`Sottotitolo`,`Contenuto` FROM `Articoli` WHERE 
    `Autore`= 'Name';";

    //Execute the query
    $result=mysqli_query($conn,$str);
    ($riga=mysqli_num_rows($result));

       //Count rows and print
        while($riga!=0){
    echo' <!-- ABOUT -->
     <section id="about" data-stellar-background-ratio="0.5">
          <div class="container">
               <div class="row">

                    <div class="col-md-6 col-sm-12">
                         <div class="about-info">
                              <div class="section-title wow fadeInUp" data-wow-delay="0.2s">
                                   <h4>'.$result['Titolo'].'</h4>
                                   <h2>'.$result['Sottotitolo'].'</h2>
                              </div>

                              <div class="wow fadeInUp" data-wow-delay="0.4s">
                                   <p>'.$result['Contenuto'].'</p>
                              </div>
                         </div>
                    </div>
<!--Templatetemo style per cambiare immagini!!!-->
                    <div class="col-md-6 col-sm-12">
                         <div class="wow fadeInUp about-image" data-wow-delay="0.6s">
                              <img src="'.$result['Cover'].'"  class="img-responsive" alt="">
                         </div>
                    </div>

               </div>
          </div>
     </section>';
     $riga--;
   }
  ?>

【问题讨论】:

标签: php html mysql


【解决方案1】:

尝试像这样更改您的 while 循环:

while ($row = mysqli_fetch_assoc($result)) {
    // Your code here
}

在 while 循环中,使用 $row[&lt;key&gt;] 而不是 $result[&lt;key&gt;],例如$row['Titolo']

您不需要任何 riga 计数器,只要获取所有结果,while 循环就会终止

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 2019-03-04
    • 2016-12-17
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多