Jump to content

using function to select database?


HDFilmMaker2112

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.