Skip to content

Instantly share code, notes, and snippets.

@neilgee
Created March 28, 2021 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilgee/cbbc4a290c8717b8130284162d2a6730 to your computer and use it in GitHub Desktop.
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
<?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