链接MySql中的数据库

时间:2013-08-14 06:07:26

标签: mysql

如何链接MySql中的2个或更多数据库,以后可以使用php页面进行访问? 这也应该可以从SWING访问,然后选择相应的数据库并根据与用户的交互进行更新。你怎么做呢?欢呼声。

1 个答案:

答案 0 :(得分:0)

这可能有助于您尝试这个

<?php

    /*
    *   The sample code below makes 2 database connections and the reference to each database connection is stored in separate variables.
    *   Whenever you connect multiple databases you have to specify the link in mysql_query function to tell PHP to run the query
    *   on the specified database.
    */

    $link1=mysql_connect("localhost","root","");
    mysql_select_db("qlets");


    $link2=mysql_connect("localhost","root","",true);
    mysql_select_db("sargodhabiz",$link2);

    $result1=mysql_query("select * from portfolio",$link1);
    show_data($result1);

    $result2=mysql_query("select * from categories",$link2);
    show_data($result2);

    mysql_close($link1);
    mysql_close($link2);


    function show_data($result){
        $x=mysql_num_fields($result);
        echo "<table border=\"1\">"; 
        while($row=mysql_fetch_array($result)){
            echo "<tr>";            
            for($i=0;$i<$x;$i++){
                echo "<td>".$row[$i]."</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
 ?>

for more read this link