#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    64
    Rep Power
    2

    Struggling to convert mysql to mysqli


    I'm not sure why i'm having such a difficult time, but my mysqli $link keeps returning null even after I have declared it as a variable.

    I'm able to connect to my database using everything inline on the same page, but my function files are producing the null errors.

    I have also added the following to the top of a function file:

    PHP Code:
    require (dirname(__FILE__).'/../*/*/*/*/*/*.php')

    $host$Host// Host name
    $username$Username// mysqli username
    $password$Password// mysqli password
    $db_name$Database// Database name

    // Connect to server and select databse.
    global $link;

    $link mysqli_connect($host$username$password$db_name)or die("cannot connect"); 
    Even with the global $link being set on the same page as the functions being used null is still being returned.

    Any help would be greatly appreciated because at this point it seems like such a pain in the *** that I should just to go back to mysql functions.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    You are using the syntax of old mysql extension. I am still getting the hang of MySqlI however i preffer using object oriented code and i use the following connection method
    PHP Code:

    $mysqli 
    = new mysqli("127.0.0.1""user""password""database"3306);
    if (
    $mysqli->connect_errno) {
        echo 
    "Failed to connect to MySQL: (" $mysqli->connect_errno ") " $mysqli->connect_error;

    sorry just read the procedural methodand your syntax was correct

    Originally Posted by nbasso713
    I'm not sure why i'm having such a difficult time, but my mysqli $link keeps returning null even after I have declared it as a variable.

    I'm able to connect to my database using everything inline on the same page, but my function files are producing the null errors.

    I have also added the following to the top of a function file:

    PHP Code:
    require (dirname(__FILE__).'/../*/*/*/*/*/*.php')

    $host$Host// Host name
    $username$Username// mysqli username
    $password$Password// mysqli password
    $db_name$Database// Database name

    // Connect to server and select databse.
    global $link;

    $link mysqli_connect($host$username$password$db_name)or die("cannot connect"); 
    Even with the global $link being set on the same page as the functions being used null is still being returned.

    Any help would be greatly appreciated because at this point it seems like such a pain in the *** that I should just to go back to mysql functions.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Are you saying that $link is null inside functions? That's what's supposed to happen. If you want to use a variable inside a function, you have to pass it in. It's a function of PHP's variable scope
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo