+ Reply to Thread
Results 1 to 6 of 6

Thread: php connect to multiple mySql Databases

  1. #1
    fomalhaut is offline x10Hosting Member fomalhaut is an unknown quantity at this point
    Join Date
    Aug 2009
    Location
    South of France near Arles
    Posts
    90

    php connect to multiple mySql Databases

    Hello.

    How many mysql databases can I create on my x10hosting account ?

    And if that is possible, with php can I connect multiple databases at the same time ? for example, can I have

    PHP Code:
    $db1 user_database11;
    $db2 user_database2;
    $dbconnect1 = new PDO("mysql:host=localhost;dbname=$db1"'user''password');
    $dbconnect2 = new PDO("mysql:host=localhost;dbname=$db2"'user''password');
    $dbconnect1->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    $dbconnect2->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    // ... 
    $count1 "SELECT count(*) FROM table1 WHERE col1 = '" $vari1 "'";
    $count2 "SELECT count(*) FROM table2 WHERE col2 = '" $vari2 "'";
    // where table1 from $db1 and table2 from $db2
    $res1 $dbconnect1->query($count1);
    $res2 $dbconnect2->query($count2); 
    Is that possible ?

    Thanks for your answers.

  2. #2
    vishal's Avatar
    vishal is offline -::-X10 Guru-::- vishal has a brilliant futurevishal has a brilliant future
    Join Date
    Nov 2009
    Location
    INDIA
    Posts
    5,250

    Re: php connect to multiple mySql Databases

    You can have only 3 sql database
    Regards ~ Vishal
    Giving Reputation (at bottom of my post ) is the best way to encourage the person who helped you on forums.

  3. #3
    misson is online now Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: php connect to multiple mySql Databases

    Some premium hosting options allow for more.

    As for accessing multiple databases from the same script, you could create a connection for each database, but this is unnecessary. The database you specify when connecting is merely the default database. When a query specifies a table but not the database, the table is looked for in the default database. To access tables in different database using the same connection, simply specify the database for each table.

    Also, if part of a query varies (i.e. you need to interpolate a variable), use prepared statements. Only use PDO::query for static queries.

    PHP Code:
    try {
      
    $query1 $db->prepare('SELECT count FROM db1.table1 WHERE col1=?');
      
    $query2 $db->prepare('SELECT count FROM db2.table2 WHERE col2=?');

      
    $query1->execute(array($val1));
      
    $query1->execute(array($val2));
    } catch (
    PDOException $exc) {
      ...

    Last edited by misson; 02-22-2010 at 03:24 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    fomalhaut is offline x10Hosting Member fomalhaut is an unknown quantity at this point
    Join Date
    Aug 2009
    Location
    South of France near Arles
    Posts
    90

    Smile Re: php connect to multiple mySql Databases

    Thank you for these precisions. I'll use 2 database, with one connect to default db.

    Thanks again.

    All the best.

  5. #5
    techmo2 is offline x10Hosting Member techmo2 is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    2

    Re: php connect to multiple mySql Databases

    Quote Originally Posted by fomalhaut View Post
    Hello.

    How many mysql databases can I create on my x10hosting account ?

    And if that is possible, with php can I connect multiple databases at the same time ? for example, can I have

    PHP Code:
    $db1 user_database11;
    $db2 user_database2;
    $dbconnect1 = new PDO("mysql:host=localhost;dbname=$db1"'user''password');
    $dbconnect2 = new PDO("mysql:host=localhost;dbname=$db2"'user''password');
    $dbconnect1->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    $dbconnect2->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
    // ... 
    $count1 "SELECT count(*) FROM table1 WHERE col1 = '" $vari1 "'";
    $count2 "SELECT count(*) FROM table2 WHERE col2 = '" $vari2 "'";
    // where table1 from $db1 and table2 from $db2
    $res1 $dbconnect1->query($count1);
    $res2 $dbconnect2->query($count2); 
    Is that possible ?

    Thanks for your answers.

    Also make sure you close the mysql connections if you are using way too many of them. I believe that the connections get closed when your script stops executing, but it is never a good idea to have a zillion open connections lurking around your app.

    Try using a single instance of every db connection you need.

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,410

    Re: php connect to multiple mySql Databases

    I usually say: one server, one connection. This is usually a good idea :P
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Can't connect to Remote MySql Server
    By ic_88_kaye in forum Programming Help
    Replies: 3
    Last Post: 01-28-2010, 07:53 AM
  2. Replies: 6
    Last Post: 01-18-2010, 07:44 PM
  3. php Can't connect to local MySQL server through socket
    By krahny in forum Programming Help
    Replies: 5
    Last Post: 12-28-2009, 02:58 PM
  4. can not connect to remote mysql server
    By rslarson4 in forum Free Hosting
    Replies: 2
    Last Post: 10-21-2009, 10:43 PM
  5. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers