Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts

    database connection with mysqli

    I am trying to do an include for my db connection with mysqli but cannot not find any examples of how do to it.

    This is what I have

    PHP Code:
    include('library/login2.php');
    login(); 
    PHP Code:
    function login()
    {
    $con mysqli_connect('localhost''stuff''stuff''stuff');

    // tried this as well $con = new mysqli(

    So what is the extra step that I need to take? I know there is something missing in my function but everything I have tried does not work.

    My db connection works directly in the page

    This is the error I am getting
    Fatal error: Call to a member function query() on a non-object in /home/content/51/10528751/html/login.php on line 49
    Line 49 is
    $result=$mysqli->query($sql);
    Last edited by harkly; 03-07-2013 at 05:02 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What's the scope of the $mysqli->query call? Mysqli is (rightfully) not a global resource like mysql was. So you cannot do this:
    PHP Code:
    function connect()
    {
        
    $mysqli = new Mysqli('localhost''user''password''db');
    }

    connect();
    $mysqli->query('...'); 
    Since $mysqli doesn't exist outside the scope of connect.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I see so the best thing is to keep it on the pages that need it?

    I have many many queries calling it in a large variety of pages

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I don't understand what you are referring to about keeping it on the pages that need it.
    What I'm referring to is that mysqli obeys the scope of code unlike the mysql library. So if you need to use it in functions you'll need to pass it in.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Location
    Earth
    Posts
    305
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I was just looking to move my db connection into an include file so when it gets changed I don't have to modify all the files just 1. Since its not a global resource as mysql it appears as though I need to keep the db connection on all the pages that require it to be there.

    I don't know how to do it any other way, have looked but to no avail, hence this posting, to see if it is possible.
    Last edited by harkly; 03-07-2013 at 07:10 PM.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,480
    Thanks
    0
    Thanked 635 Times in 625 Posts
    The quickest solution is to define the database connection as global inside each of the functions tat needs to use if. For example:

    Code:
    function login() 
    { 
    global $mysqli;
    $mysqli = mysqli_connect('localhost', 'stuff', 'stuff', 'stuff'); 
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Globalizing a variable can create horrific debugging.
    But it's true, it is the easiest fix.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New Coder
    Join Date
    Feb 2013
    Posts
    39
    Thanks
    14
    Thanked 0 Times in 0 Posts
    I think what you are looking for is

    PHP Code:
    require_once('somefile.php'); 
    Place it at the start of your PHP on each page that needs a connection.


    Inside of somefile.php:
    PHP Code:
    <?php
    $connection 
    mysqli_connect('localhost','stuff','stuff','stuff');
    if (
    mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }
    ?>


  •  

    Posting Permissions

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