0

hi i am using mysql for my database. just i try to connect mysql db through looping but it failed , i cannot , Is there is any otherway to do this

my trial code is this

while($row = mysql_fetch_array($query)) {

    $temp = "db".$row["listid"];
    $temp = mysql_connect("localhost","root","", true); 
    mysql_select_db($row["databasename"],$temp);
}

is it any other way to do this.

2
  • because i am going to integrate all my application into one admin control . just one login through this it manage all database related action i had more than 6 database its not tough to write seperate mysql_connect statement for evey database ,it is just try. in my knowledge i know it is not possible , but some genius have a idea for this.. so i am asking this question. Commented Jun 23, 2011 at 7:38
  • OK so you've got one server with multiple databases? If so see my answer below. Commented Jun 23, 2011 at 7:41

2 Answers 2

1

Try with:

$res = mysql_query($query);
while($row = mysql_fetch_array($res)) {
  $name = "db".$row["listid"];
  $temp = mysql_connect("localhost","root","", true) or die('Could not connect: ' . mysql_error()); 
  mysql_select_db($row["databasename"],$temp);
  $res = mysql_query($query, $temp);
}

and tell us your error - but, as the first comment, this is highly NOT recommended

Sign up to request clarification or add additional context in comments.

5 Comments

thank you for your reply Tudor Constantin -- mysql_connect statement does not show any specific error , the error is "mysql_fetch _array supplied argument is not validargument or no database connection found" .for an example i had 5 data base through looping i want to connect $db1 for database 1,$db2 for database2 like that . if i use the above code it just create only one connection in the variable name $temp.
I modified the code - it sets an initial $res as a resultset, then in while it changes it - Is this what you wanted? Maybe you need another while inside the main one
The $res is overwritten inside the while loop.
Still i receive the same error "mysql_query(): supplied argument is not a valid MySQL-Link resource" i call database 4 by $select = "select * from ".$tablename.""; $query = mysql_query($select,$db4) or die("Error(". mysql_error().")". mysql_error());
have connected to the DB before the first query?
0

You only require ONE database connection.

Once you connect to a database it is available for the life of that page (request).

If you require to switch databases that will also use the same connection (unless different credentials are required.

If it's for cross database queries if they are on the same MySQL server and the user for the initial connection has sufficient privileges, then you can prefix database tables with the database name.

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.