Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //1. Create a db connection
- $dbhost = "localhost";
- $dbuser = "widget_cms";
- $dbpass = "secretpassword";
- $dbname = "widget_corp";
- $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
- //Test if connection occured
- if(mysqli_connect_errno()) {
- die("Database connection failed: " .
- mysqli_connect_error() .
- " (" . mysqli_connect_errno() . ")"
- );
- } else {
- $connection_status = "true";
- }
- ?>
- <?php
- //2. Perform db query
- $query = "SELECT * FROM subjects";
- $result = mysqli_query($connection, $query);
- //Test if there was a query error
- if(!$result) {
- die("DB query failed.");
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>connecting to mysql with php</title>
- </head>
- <body>
- <?php
- if(isset($connection_status)) {
- echo "Connection successfull. <hr />";
- }
- ?>
- <?php
- //3. Use returned data (if any)
- //while it can fetch assign it to $row variable ( mysqli_fetch_row($result) ) - is a condition
- while( $row = mysqli_fetch_row($result) ) {
- //output data from each row
- var_dump($row);
- echo "<hr />";
- }
- ?>
- </body>
- </html>
- <?php
- // 5. Close database connection
- mysqli_close($connection);
- ?>
Add Comment
Please, Sign In to add comment