Need help with this function

Discussion in 'PHP' started by phantom, Mar 12, 2011.

  1. #1
    I am trying to make function that opens a db connection and am having trouble.

    The function will be used to decide which database to connect to give the value of the $db var.

    My code so far is

    
    $db = 1;
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);
    
    global $d;
    global $sd;
    global $conerror;
    
    function getdb($db=1){
    $dbh = 'localhost';
    $dbu = 'dbuser';
    $dbp = 'sbpass';
    $dbn = 'dbname';
    
    
    $conerror = true;
    if($db == 1){
    $d = mysqli_connect( $dbh , $dbu , $dbp) or $conerror = 'failure to connect';
    $sd = mysqli_select_db( $d, $dbn ) or $conerror = 'can not select db';//die("Error getting db: <br><br>".mysql_error());
    }
    
    if (mysqli_connect_errno()) {
      $conerror = false;;
    }
    return $conerror;
    }
    
    $getdb = getdb($db);
    if ($getdb){
    $q = "select *  from projects where pid='14' order by pid limit 1 ";
    $result = mysqli_query($d, $q, MYSQLI_USE_RESULT) ;
    while ( $grow = mysqli_fetch_assoc($result) ) {
    $tz = $grow['tz'];
    
    echo $tz;
    }
    
      //do stuff here
    
    }
    
    
    Code (markup):

    Any ideas as to what I am doing wrong or have a better solution all together?

    Thanks in advance.
     
    phantom, Mar 12, 2011 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    I don't get your real problem? please explain in full! :)
     
    EricBruggema, Mar 14, 2011 IP
  3. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Nevermind I figured it out!
     
    phantom, Mar 14, 2011 IP
  4. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you're diving into OO PHP , I would strongly suggest (only if u r open to suggestions) to use mysqli , it is more complex than mysql_connect but, it is efficient.
     
    eleetgeek, Mar 14, 2011 IP