Ricky Layman Ricky Layman - 6 months ago 19
SQL Question

Simple PHP embedded in some really Simple HTML

I'm working on getting a Raspberry pi project online. I've got some sample code writing a MYSQL database, and I've successfully got the domain pointer set up and I can get the data online using Apache. However, when I found some sample code online with ways to do these things it does not completely work for me. However, if I directly access a .php file with the database information it works.

Can anyone help me understand where I'm going wrong on the HTML side of things and why it won't show me the information correctly?

This is what I get.

enter image description here]

My code is this:
connectdb.php

<?PHP
DEFINE ('server' = '127.0.0.1');
DEFINE ('user_name' = 'root');
DEFINE ('password' = 'xxxxxxxx');
DEFINE ('database' = 'website_database');

$db_handle = mysql_connect(server, user_name, password, database);
$db_found = mysql_select_db($database, $db_handle);

?>


My html site is:

<html>
<head>
<title>Display Data into DB</title>
<style type="text/css">
table {
border: 2px solid red;
background-color: #FFC;
}
th {
border-bottom: 5px solid #000;
}

td {
border-bottom: 2px solid #666;
}
</style>
</head>
<body>

<a href="database.php">blah</a>

<h1> Display Data from DB </h1>

<?php
include('connectdb.php');

$sqlget = "SELECT * FROM templog";
$sqldata = mysqli_query($db_handle, $sqlget) or die('error getting db');

echo "<table>";
echo "<tr><th>ID</th><th>Date</th><th>Time</th>";

while($column = mysqli_fetch_array($sqldata, MYSQLI_ASSOC))
{
echo "<tr><td>";
echo $column['date'];
echo "</td><td>";
echo $column['time'];
echo "</td></tr>";
}

echo "</table>";

?>

</body>
</html>


Any help would be great. I'm very new to this and just trying to get started with some basic functions. I'll literally have less than 15 functions passed through here so I'm not looking for many bells and whistles, just a little display and eventually a little input.

Answer

Suggesting that you use MySQLi as mysql is depreciated.

connectdb.php

$mysqli = newMySQLi('localhost','root','xxxxxxx','website_database');

your main driver

<head>
<title>Display Data into DB</title>
<style type="text/css">
    table {
border: 2px solid red;
background-color: #FFC;
}
th {
border-bottom: 5px solid #000;
}

td {
border-bottom: 2px solid #666;
}   
</style>
</head>
<body>

<!-- no reason to href this    <a href="database.php">blah</a> --->

<h1> Display Data from DB </h1>

<?php
include('connectdb.php');

$sqlget = "SELECT * FROM templog";
$results = $mySQLi->query($sqlget);

echo "<table>";
echo "<tr><th>ID</th><th>Date</th><th>Time</th>";

while($column = $results->fetch_assoc()) 
    {
    echo "<tr><td>";
    echo $column['date'];
    echo "</td><td>";
    echo $column['time'];
    echo "</td></tr>";
    }

echo "</table>";

?>

</body>
Comments