Reputation: 11
<?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>
Upvotes: 0
Views: 55
Reputation: 413
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
}
}
?>
Upvotes: 1