0

Friends I have a big problem. I have several database connections. I have to use those connections dynamically in a mysql_query(). For example,

$db1=mysql_connect('port','username','password') or die("cannot connect to the database");
@mysql_select_db('db1') or die("Unable to select Database");

$db2=mysql_connect('port','username','password') or die("cannot connect to the database");
@mysql_select_db('db2') or die("Unable to select Database");

which connection to use is defined from a switch case.

switch(type){
case "1":
$link="$db1";
break;
case "2":
$link="$db2";
break;
}

my mysql_query is like this.

mysql_query("DELETE FROM table1 WHERE id='2'",$link);

unfortunately it's not working it says supplied argument is not a valid MySQL-Link resource

I have tried following also but no luck

mysql_query("DELETE FROM table1 WHERE id='2'".','.$link);

How can i do this? Any idea ??

1
  • 1
    Remove the quotes from $link="$db1"; and $link="$db2"; Commented Sep 7, 2012 at 9:33

1 Answer 1

3

Don't transform the connection ids to strings in the switch statement by enclosing them with "! Just use the following:

switch(type){
  case "1":
    $link=$db1;
    break;
  case "2":
    $link=$db2;
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.