Created
March 28, 2021 00:24
-
-
Save neilgee/cbbc4a290c8717b8130284162d2a6730 to your computer and use it in GitHub Desktop.
Connecting PHP to MySQL and output data stored in rows from a table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Step1 | |
$db = mysqli_connect('localhost','root','root','database_name') | |
or die('Error connecting to MySQL server.'); | |
?> | |
<html> | |
<head> | |
</head> | |
<body> | |
<h1>PHP connecting to MySQL</h1> | |
<?php | |
//Step2 | |
$query = "SELECT * FROM table_name"; | |
mysqli_query($db, $query) or die('Error querying database.'); | |
//Step3 | |
$result = mysqli_query($db, $query); | |
while ($row = mysqli_fetch_array($result)) { | |
echo $row['FirstName'] . ' ' . $row['LastName'] . ': ' . $row['Email'] . ' ' . $row['City'] .'<br />'; | |
} | |
//Step 4 | |
mysqli_close($db); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment