Last active
November 4, 2016 00:49
-
-
Save edavis25/49542b9026115aa7809559ddd4fead84 to your computer and use it in GitHub Desktop.
PHP Database Connection
This file contains hidden or 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 | |
// 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