Jump to content
Welcome, Guest
Existing user? Sign In

Sign In



Sign Up
The MatriX
  • Welcome To Ghbsys
  • CS GO Streaming Version is released. Have fun streaming while cheating!
  • Have a Payment Issue? Send us a Support ticket.
  • Make a thread if you need support or join our discord for live support.
  • Have Suggestions? Make a thread and you'll earn Ghbsys Points for implemented suggestions.
  • Join our discord to stay well connected! Don't forget to integrate your discord to the site
  • Welcome to [GHB] - GAmEhAcKbAsTaRdS Forum

    Welcome to [GHB] - GAmEhAcKbAsTaRdS Forum, like most online communities you must register to view or post in our community, but don't worry this is a simple free process that requires minimal information for you to signup. Be apart of [GHB] - GAmEhAcKbAsTaRdS Forum by signing in or creating an account.
    • Start new topics and reply to others
    • Subscribe to topics and forums to get email updates
    • Get your own profile page and make new friends
    • Send personal messages to other members.

    How to connect to your database


    DirectNULL
     Share

    Recommended Posts

    Hello everybody,

     

    Now a days much people are still using MySQL.
    In these days you need to protect your online system so much as it can.

     

    It's adviced to use MySQLi or PDO instead of MySQL.

    Since PHP 5 it's not possible anymore to use MySQL but people got still older servers or are programming at localhost (using Xampp/lamp/IIS).

     

    What i will explain in this tutorial

    I will explain the difference and why it's safer to use MySQLi or PDO (I Prefer PDO).

     

    PDO explaration and code examples

    This is very usefull when you are programming OOP. For security reaons when you are talking about a huge system what you are programming (Need to think when you got money balance and payments) its almost required to use PDO becuase with PDO you can create unprepared statements (Statements or query's that are able to catch every call and decline everything that is not allowed).

     

    A example connection with PDO

    <?php
    
    $host = "localhost";
    $username = "root";
    $password = "welcome";
    $database = "php";
    
    $conn = new PDO('mysql:host=$host;dbname=$database', $username, $password);
    
    ?>  
    

    This is a connection without handling any error's. If wou want to handle some error's you could use the following code:
    <?php
    
    try {
        $host = "localhost";
        $username = "root";
        $password = "welcome";
        $database = "php";
    
        $conn = new PDO('mysql:host=$host;dbname=$database', $username, $password);
    
        foreach($conn->query('SELECT * from TABLE') as $row) {
            print_r($row);
        }
    
        $conn= null;
    
    } catch (PDOException $e) {
        echo "There is a error called: " . $e->getMessage() . "<br/>";
        die();
    }
    
    ?>  
    

    The method that's used here above called try and catch is very usefull. With a try and catch you can check if its possible to reach. If not the code would not be published or used. What we are saying right here is if the query gets a result then we will show everything about that table. If not PDO will return some error's becuase it can't reach the database or table. PDO calls that a Exception.

     

    I used (PDOException $e) the variabel $e is not a verified variabel for PDOException. You can use your own variables right there.

     

     

    MySQLi explaration and code examples

    With MySQLi you can replace MySQL for the easy things when you dont want to protect it 100%.
    You will never be safe while using MySQLi or you must do so much checks over the variables to and from the database.

     

    Down here a example of a connection to the database with error handling in MySQLi

    <?php
    
    $host = "localhost";
    $username = "root";
    $password = "welcome";
    $database = "php";
    
    $mysqli = new mysqli($host, $username, $password, $database);
    
    if (mysqli_connect_errno()) {
         printf("Connect failed: %s\n", mysqli_connect_error());
         exit();
    }
    
    ?>
    

    I hope you all liked this tutorial.
    This is my first one @ GHB so tips are welcome.

    Do you see a miss-spell or a wrong explanation please send me a private message I will appreciate it.

     

    Goodluck programmers! smiley_emoticons_unknownauthor_na.gif

     

    Greetz SeriiousGamerr

    Link to comment
    Share on other sites

    Guest
    This topic is now closed to further replies.
     Share

    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...