PHP MySQL Select Data
- Programming
- 19 June 2022
- 1615 Views
- Sathiyamoorthy V
1. PHP MySQL Select Data ( MySQLi Object-Oriented )
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row['id']. " - Name: " . $row['firstname']. " " . $row['lastname']. "<br>";
}
}
else
{
echo "0 results";
}
$conn->close();
?>
2. PHP MySQL Select Data ( MySQLi Procedural )
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql = "SELECT * FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
// output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "id: " . $row['id']. " - Name: " . $row['firstname']. " " . $row['lastname']. "<br>";
}
}
else
{
echo "0 results";
}
mysqli_close($conn);
?>
PHP MySQL Select Data
https://blogbyte.in/blog-details/?cid=2&pid=38
LEAVE A COMMENT