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.
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.