HDFilmMaker2112 Posted June 23, 2011 Share Posted June 23, 2011 I'm trying to set-up a function for me to choose which database to use, by place a call to the function with either number 1 or 2. $db_name="zyquo_donors"; // Database name $db_name2="zyquo_investors"; $tbl_name="donors"; // Table name $tbl_name2="donors_credits"; $tbl_name3="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); function db_select($db="1"){ if($db=="1"){ mysql_select_db("$db_name")or die("cannot select DB"); } elseif($db=="2"){ mysql_select_db("$db_name2")or die("cannot select DB"); } } and on the page I have: db_select("2"); The result is: cannot select DB Link to comment https://forums.phpfreaks.com/topic/240196-using-function-to-select-database/ Share on other sites More sharing options...
QuickOldCar Posted June 23, 2011 Share Posted June 23, 2011 My best response would be to learn how to write functions. http://php.net/manual/en/language.functions.php Functions can be made in a few ways, but usually you need to either echo or return the result. function db_select($db){ if($db == 1) { $myselect = mysql_select_db("$db_name")or die("cannot select DB"); } elseif($db == 2) { $myselect = mysql_select_db("$db_name2")or die("cannot select DB"); } else { $myselect = "There was a problem selecting a database"; } return $myselect; } The above is just a fast example. Link to comment https://forums.phpfreaks.com/topic/240196-using-function-to-select-database/#findComment-1233796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.