Skip to content

Instantly share code, notes, and snippets.

@edavis25
Last active November 4, 2016 00:49
Show Gist options
  • Save edavis25/49542b9026115aa7809559ddd4fead84 to your computer and use it in GitHub Desktop.
Save edavis25/49542b9026115aa7809559ddd4fead84 to your computer and use it in GitHub Desktop.
PHP Database Connection
<?php
// Create MySQL connection. Params=(host, DB username, DB passwword, name of Database)
$db = mysqli_connect('localhost','username','password','db-name')
or die('Error connecting to MySQL server.');
// Create query
$query = "SELECT * FROM TABLE"; //Add whatever query desired
// Query database using variables
mysqli_query($db, $query) or die('Error querying database.');
// Store results
$result = mysqli_query($db, $query);
// Iterate results
while ($row = mysqli_fetch_array($result))
{
// Do stuff with the row. Get each column using name and echo or build
// an html table string, etc. Ex: (prints just 1 column called Date in DB)
echo $row['Date'];
}
// Close connection to database
mysqli_close($db);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment