Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* This file contains several functions to make using databases in PHP easy.
- .. Function connectDb lets users easily connect to a database.
- .. Function query executes a query on the selected database and returns the result.
- .. Function closeConnection closes the connection with the database.
- .. Function createTable creates an HTML-table from the results of a query.
- */
- function connectDb($database, $dbhost='localhost', $user='root', $pass='') {
- $db = mysqli_connect($dbhost, $user, $pass, $database);
- if (mysqli_connect_errno()) {
- die('Connection failed: ' . mysqli_error() . ' (' . mysqli_errno() . ')');
- }
- return $db;
- }
- function query($db, $query) {
- mysqli_query($db, $query) or die('Error performing query.');
- return mysqli_query($db, $query);
- }
- function closeConnection($db) {
- mysqli_close($db);
- }
- function createTable($result, $border=1, $header='Results') {
- $buffer = array();
- while ($row=mysqli_fetch_assoc($result)){
- $buffer[] = $row;
- }
- echo '<table border="' . $border . '">';
- echo "<tr><th colspan=\"100%\">$header</th></tr>";
- echo '<tr>';
- foreach ($buffer[0] as $a => $a_value) {
- echo '<td>' . $a . '</td>';
- }
- echo '</tr>';
- foreach ($buffer as $row) {
- echo '<tr>';
- foreach ($row as $x) {
- echo '<td>' . $x . '</td>';
- }
- echo '</tr>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement