1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Passing Connection Object

Discussion in 'PHP' started by trixs, Feb 9, 2008.

  1. #1
    Hey Everyone,

    Im hoping someone can help me figure this out because its starting to upset me lol..

    Anyway, all im trying to do is pass a mysqli connection to other objects.
    So you create a basic connection - No issue.
    // Include connection info - this needs to be outside of view
    include("info.inc");
    
    // Create connection
    $mysqli = new mysqli($host, $user, $password, $database);
    
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }
    PHP:
    Now of course I want to pass this connection to various objects to actually do something with it. So I try:

    
    require_once('asdf.php');
    $lol = new lol($mysqli);
    
    PHP:
    Passing the object this way requires a constructor.. Ok:

      
      function __construct($mysqli) {
    
           $this->mysqli = $mysqli;
    
           if (mysqli_connect_errno()) {
              printf("Connect failed: %s\n", mysqli_connect_error());
              exit();
           }        
    
       }
    
    PHP:
    Now this will not fail but when I try to actually use the connection it tells me the result is a non object. IT would be truly nice if I could just use a pointer! I have also tried to use globals for the connection but that doesnt seem to work with php and objects.

    Anyone know what I am doing wrong?
     
    trixs, Feb 9, 2008 IP
  2. trixs

    trixs Peon

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Figure it out!

    If anyone is interested here is what I learned:

    I had a table with a field called "index" as this program has to do with the stock market. Apparently mysql and php do not like a field name 'index'!

    the code above works fine for anyone having problems connecting.
     
    trixs, Feb 9, 2008 IP