0% found this document useful (0 votes)
29 views2 pages

PHP MySQL Database Connection Example

This PHP script connects to a MySQL database hosted on the same server and retrieves data from a table named 'nodemcu_table'. It checks for a successful connection and then queries the database for specific columns (id, val, val2, date, time). The results are displayed in a formatted manner, or a message is shown if there are no results.

Uploaded by

firebase lontec
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

PHP MySQL Database Connection Example

This PHP script connects to a MySQL database hosted on the same server and retrieves data from a table named 'nodemcu_table'. It checks for a successful connection and then queries the database for specific columns (id, val, val2, date, time). The results are displayed in a formatted manner, or a message is shown if there are no results.

Uploaded by

firebase lontec
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Code Written by Rishi Tiwari

// Website:- [Link]
// Reference:- [Link]
//
//

<?php

$host = "localhost"; // host = localhost because database


hosted on the same server where PHP files are hosted
$dbname = "sumodb"; // Database name
$username = "sumouser"; // Database username
$password = "Tr[~b>)wwac6Ze~G"; // Database password

// Establish connection to MySQL database


$conn = new mysqli($host, $username, $password, $dbname);

// Check if connection established successfully


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

else { echo "Connected to mysql database. <br>"; }

// Select values from MySQL database table

$sql = "SELECT id, val, val2, date, time FROM nodemcu_table"; // Update your
tablename here

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

echo "<center>";

if ($result->num_rows > 0) {

// output data of each row


while($row = $result->fetch_assoc()) {
echo "<strong> Id:</strong> " . $row["id"]. " &nbsp <strong>val:</strong> "
. $row["val"]. " &nbsp <strong>val2:</strong> " . $row["val2"]. " &nbsp
<strong>Date:</strong> " . $row["date"]." &nbsp <strong>Time:</strong>" .
$row["time"]. "<p>";

}
} else {
echo "0 results";
}

echo "</center>";
$conn->close();

?>

You might also like