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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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