www.webdeveloper.com
Results 1 to 3 of 3

Thread: Upgrading to mysqli?

  1. #1
    Join Date
    Aug 2013
    Posts
    2

    Wink Upgrading to mysqli?

    I have my script and i am hoping to update to mysqli because I hear that the onld Mysql_connect functions will not work in the future, i am hoping that aperson can translate this script to Mysqli:
    PHP Code:
    define("DB_USERNAME""*****t");
        
    define("DB_PASSWORD""*****");
        
    define("DB_DATABASE""*****");
        
    define("DB_SERVER""127.0.0.1");

        
    $db_handle mysql_connect(DB_SERVERDB_USERNAMEDB_PASSWORD);

        
    $db_found mysql_select_db(DB_DATABASE$db_handle);

        if (
    $db_found || true) {

        
    $SQL "SELECT * FROM members";
        
    $result mysql_query($SQL) or die(mysql_error());

         while ( 
    $row mysql_fetch_assoc($result) ) {

            
    $id $row['member_id'];
            
    $username $row['username'];
            
    $password $row['password'];
            
    $rank $row['rank'];

            
        }

        
    mysql_close($db_handle);

        } else {
        echo 
    "Database NOT Found " $db_handle;
        }

        
    ?> 
    Help is much appreciated.
    Last edited by NogDog; 08-08-2013 at 11:41 PM. Reason: added [php] tags around code, obfuscated password stuff

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Untested (and I use PDO these days, so no guarantees):
    PHP Code:
    <?php
    define
    ("DB_USERNAME""*****t");
    define("DB_PASSWORD""*****");
    define("DB_DATABASE""*****");
    define("DB_SERVER""127.0.0.1");

    $mysqli = new mysqli(DB_SERVERDB_USERNAMEDB_PASSWORDDB_DATABASE);
    if (
    $mysqli == false) {
        throw new 
    Exception("DB connection failed: ".mysqli_connect_error());
    }

    $SQL "SELECT * FROM members";
    $result $mysqli->query($SQL) or die($mysqli->error);

    while ( 
    $row $result->fetch_assoc($result) ) {
        
    $id $row['member_id'];
        
    $username $row['username'];
        
    $password $row['password'];
        
    $rank $row['rank'];
    }

    $myssqli->close();
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2013
    Posts
    2
    Quote Originally Posted by NogDog View Post
    Untested (and I use PDO these days, so no guarantees):
    PHP Code:
    <?php
    define
    ("DB_USERNAME""*****t");
    define("DB_PASSWORD""*****");
    define("DB_DATABASE""*****");
    define("DB_SERVER""127.0.0.1");

    $mysqli = new mysqli(DB_SERVERDB_USERNAMEDB_PASSWORDDB_DATABASE);
    if (
    $mysqli == false) {
        throw new 
    Exception("DB connection failed: ".mysqli_connect_error());
    }

    $SQL "SELECT * FROM members";
    $result $mysqli->query($SQL) or die($mysqli->error);

    while ( 
    $row $result->fetch_assoc($result) ) {
        
    $id $row['member_id'];
        
    $username $row['username'];
        
    $password $row['password'];
        
    $rank $row['rank'];
    }

    $myssqli->close();

    In case anyone is finding this useful I tested the script and made some tweaks to it so it works. I will post the working script:

    <?php
    define("DB_USERNAME", "root");
    define("DB_PASSWORD", "Hunter123");
    define("DB_DATABASE", "adventure_of_dragons");
    define("DB_SERVER", "127.0.0.1");

    $mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if ($mysqli == false) {
    throw new Exception("DB connection failed: ".mysqli_connect_error());
    }

    $SQL = "SELECT member_id, username, password, rank FROM members";
    $result = $mysqli->query($SQL) or die($mysqli->error);

    while ( $row = $result->fetch_array() ) {
    $id = $row['member_id'];
    $username = $row['username'];
    $password = $row['password'];
    $rank = $row['rank'];
    }

    $mysqli->close();

    ?>

    Thanks for the help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles