PHP未定义的索引数据库问题

时间:2017-11-14 10:07:45

标签: php html mysql html-table

我在显示数据库表时遇到问题。我的数据库表(活动)具有以下值,ID(主键),类型,标题,描述和图片。我试图以表格格式检索和显示标题,描述和图片,但面临错误

  

"注意:未定义索引:标题,注意:未定义索引:描述   并注意:未定义索引:图片"

我无法解决它。 以下是我的PHP代码:

      <div class="col-md-4 col-md-offset-1">
           <div id="content">   
               <table><?php 
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "sentosa_resort";

                $conn = new mysqli($servername, $username, $password, 
               $dbname);
               // Check connection
               if ($conn->connect_error) {
                   die("Connection failed: " . $conn->connect_error);
               } 

                $query = "SELECT title,description,picture FROM activites"; 
                $result = $conn->query($sql);;

                 // start a table tag in the HTML

                while($row=mysqli_fetch_array($result)){
                    $title = $row['title'];
                    $description = $row['description'];
                   $picture = $row['picture'];
                    echo "<tr>";
                    echo "<td>" . $title . "</td>";
                    echo "<td>" . $description . "</td>";
                     echo "<td>" . $picture . "</td>";
                    echo "</tr>";
                }
                ?></table>
                </div>
          </div>   

4 个答案:

答案 0 :(得分:0)

请检查您的代码,以获取第16行中的额外分号...

OR

您可以使用mysqli_fetch_row

 <div class="col-md-4 col-md-offset-1">
                       <div id="content">   
                           <table>
        <?php 
                            $servername = "localhost";
                            $username = "root";
                            $password = "";
                            $dbname = "sentosa_resort";

                            $conn = new mysqli($servername, $username, $password, 
                           $dbname);
                           // Check connection
                           if ($conn->connect_error) {
                               die("Connection failed: " . $conn->connect_error);
                           } 

                            $query = "SELECT title,description,picture FROM activites"; 
                            $result = $conn->query($query);

                             // start a table tag in the HTML

                            while($row=mysqli_fetch_row($result)){
                                $title = $row['0'];
                                $description = $row['1'];
                               $picture = $row['2'];
                                echo "<tr>";
                                echo "<td>" . $title . "</td>";
                                echo "<td>" . $description . "</td>";
                                 echo "<td>" . $picture . "</td>";
                                echo "</tr>";
                            }
                            ?>
    </table>
                            </div>
                      </div>   

答案 1 :(得分:0)

您的代码中存在错误,即:

 $query = "SELECT title,description,picture FROM activites";
 $result = $conn->query($sql);; // here is error

请更改上面的错误行,如下所示:

$result = $conn->query($query);

解决方案:将$ sql更改为$ query,因为您的查询字符串存储在$ query变量而非$ sql中并删除了额外的分号

答案 2 :(得分:0)

我已更改您的代码,请检查

<div class="col-md-4 col-md-offset-1">
           <div id="content">   
               <table><?php 
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "sentosa_resort";

                $conn = mysqli_connect($servername, $username, $password, 
               $dbname);


                $query = "SELECT title,description,picture FROM activites"; 
                $result = mysqli_query($conn,$query);    


                while($row=mysqli_fetch_assoc($result)){
                    $title = $row['title'];
                    $description = $row['description'];
                   $picture = $row['picture'];
                    echo "<tr>";
                    echo "<td>" . $title . "</td>";
                    echo "<td>" . $description . "</td>";
                     echo "<td>" . $picture . "</td>";
                    echo "</tr>";
                }
                ?></table>
                </div>
          </div>   

答案 3 :(得分:-1)

您收到这些通知是因为您使用的是mysqli_fetch_array(),而是需要使用mysqli_fetch_assoc()

如果您想继续使用mysqli_fetch_array(),请将代码更改为:

$title = $row[0];
$description = $row[1];
$picture = $row[2];