0
<?php

Used to display errors

  ini_set("display_errors",1);

error_reporting(E_ALL);

$servername = "localhost";
$username = "1702520";
$password = "password";
$dbname = "myDB";

I'm new to php and I'm not sure if the code bellow is necessary as i have been trying to make this page using basic online tutorials

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

I'm trying to create a database that contains a name, an image, description and a ID

 // Create database
 $sql = "CREATE DATABASE myDB";
 if ($conn->query($sql) === TRUE) {

    echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; }

Creating the database table

  $sql = "CREATE TABLE Data(
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  hname VARCHAR(30) NOT NULL,
  himage VARCHAR(30) NOT NULL, 
  hdesc VARCHAR(30) NOT NULL
   )";

Inserting data into the database (sorry I'm new to php so if there is a more efficient way to insert data into a database i would be happy to know about it)

 $sql = "INSERT INTO Data (id, hname,himage,hdesc)
 VALUES (1,name1,image address,desc1)"

$sql = "INSERT INTO Data (id, hname,himage,hdesc)
VALUES (2,name2,image address,desc2)"

$sql = "INSERT INTO Data (id, hname,himage ,hdesc)
VALUES (3,name3,image address, desc3)"

checks if table was created properly

if ($conn->query($sql) === TRUE)
{ echo "Table Data created successfully"; } else {  echo "Error creating table: " . $conn->error;  }

gets the data from the database

 $result = mysql_query("SELECT * FROM Data ");
 $conn->close();



?> 

trying to display all the data in the database

<html>

 <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
  <thead>
    <tr>
      <th>id</th>
      <th>Name</th>
      <th>image</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <?php
      while( $row = mysql_fetch_assoc( $result ) ){
        echo
        "<tr>
          <td>{$row\['id'\]}</td>
          <td>{$row\['hname'\]}</td>
          <td>{$row\['himage'\]}</td>
          <td>{$row\['hdesc'\]}</td> 
        </tr>\n";
      }
    ?>
  </tbody>
</table>
 <?php mysql_close($connector); ?>
</body>


</html>
1
  • You created connection using mysqli and later you are trying to show data using mysql way. Don't mess up with mysql and mysqli, instead I suggest use the later. See how you can select data from database and show on your html table w3schools.com/php/php_mysql_select.asp Commented Sep 16, 2017 at 15:35

1 Answer 1

1

Your Question is Unclear, but I think mean the Data are not displayed from the DataBase?

use foreach $row. and you didnt echo the output. I have re written the Database output code Please Replace this with your Old Code.

<?php
      while( $row1 = mysqli_fetch_assoc( $result ) ){
        foreach ($row1 as $row){
?>
        <tr>
          <td><?php $row['id'] ?></td>
          <td><?php $row['hname'] ?></td>
          <td><?php $row['himage'] ?></td>
          <td><?php $row['hdesc'] ?></td> 
        </tr>
<?php
        }
      }
?>
Sign up to request clarification or add additional context in comments.

5 Comments

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
He didn't mentioned the PHP Version and I have correct the mysql to mysqli Thanks for informing.
glad you understood and change your code. +1 vote for that.:)
Thank you @BeingSunny ;) 😍
@mmmmmkeyisstuckmmmmmmmmmm So have you got the output? then vote me up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.