0

I am having issues with displaying the database table. My database table (activities) have the following value, ID (Primary Key), type, title, description and picture. I am trying to retrieve and display title,description and picture in a table format but is facing an error

"Notice: Undefined index: title, Notice: Undefined index: description and Notice: Undefined index: picture"

which I am unable to solve it. Below are my php code:

      <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>   
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tan Leon
  • 61
  • 8

4 Answers4

0

Kindly check your code for an extra semi colon in line number 16...

OR

You can use 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>   
Jegadesh B S
  • 689
  • 1
  • 6
  • 14
  • If he uses mysqli_fetch_array without specifying a second parameter, MYSQLI_BOTH is implicit, so he'll get both an index based and associative based array, so what the OP did is right. – Amarnasan Nov 14 '17 at 10:21
0

There is error in your code and that is:

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

Please do change change the above error line similar to below:

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

Solution: Changed $sql to $query because your query string is stored in $query variable not $sql and removed extra semicolon

Yubaraj Shrestha
  • 864
  • 1
  • 10
  • 16
0

I have changed your code please check it

<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>   
Jeevan
  • 1
  • 2
-1

You are getting those Notices because you are using mysqli_fetch_array(), instead you need to use mysqli_fetch_assoc().

If you want to continue using mysqli_fetch_array() then change the code to:

$title = $row[0];
$description = $row[1];
$picture = $row[2];
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35