Skip to content
Snippets Groups Projects

MySQL Connect

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Zandor Smith

    My usage:

    Place connect.php in a folder called includes. The includes folder is in the root of the web application.

    Then in any file I need to execute an SQL query to the database: include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/connect.php";

    Extra points to address

    For using the $mysqli variable for executing queries and such (using OOP) in PHP, read the following articles;

    For executing queries: http://php.net/manual/en/mysqli.query.php Note: I prefer to use Object Oriented style instead of the Procedural style.

    For more information, consult: http://php.net/manual/en/class.mysqli.php

    connect.php 299 B
    <?php
    $mysqli = mysqli_connect("<HOST>", "<USERNAME>", "<PASSWORD>", "<DATABASE>");
    if (!$mysqli) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment