Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // The script connects to the database, run the query and display the output on simple paragraphs.
- // Create function with connection settings (its recommended to put this info into another file)
- function connect() {
- // Here we try to connect to database with this credentials:
- $hostname = 'localhost'; // Your hostname or server name
- $username = 'root'; // User name for mysql
- $password = ''; // Password for mysql (in this case is emtpy)
- // Create connection
- $link = mysqli_connect($hostname, $username, $password);
- // if cannot establish connection...
- if (!$link) {
- // display this message:
- echo "Error trying to connect with database.";
- // and exit php script
- exit();
- }
- return $link;
- }
- // Define variable with function to connect to the db
- $link = connect();
- // Define values for query
- $dbname = 'database_name'; // A database name
- $tblname = 'table_name'; // A table name
- // Define a query that you want
- $query = "SELECT * FROM $dbname.$tblname;";
- // Condition if cannot run query
- if (!$result = mysqli_query($link, $query)) {
- // display error message you want
- echo "ERROR - Cannot execute query. Please check your data.";
- // and exit from the script
- exit();
- }
- // In case query run successfully
- else {
- // assign value for each row
- while ($row = mysqli_fetch_array($result)) {
- // and display a paragraph (you can use a div, table, etc), specifying the column name for each result you want to display
- // structure example: .$row["column_name"].
- echo "<p>COLUMN NAME: ".$row["column_name_1"]."<br>OTHER COLUMN: ".$row["column_name_2"]."</p>";
- }
- }
- // Close mysql connection
- mysqli_close($link);
- // and exit the script
- exit();
- ?>
Add Comment
Please, Sign In to add comment