猿问

如何在具有更多mysql数据库连接的php页面中实时工作

我有三个具有相同结构(相同表)的 mysql 数据库。然后我有一个查询,它从每个数据库返回不同的结果。


我想要一个 php 页面,其中有单选按钮、列表框等(没有提交按钮),我将在其中选择数据库(DB1/DB2/DB3),然后根据所选数据库查看结果(我想要它是实时的,没有提交按钮)。


我有的:


索引.php


 <!DOCTYPE html>

    <html>

    <head>

    <meta charset="utf-8" />

    </head>


    <?php

    include_once ('connection_db_1.php');

    ?>


    <body>

        <form action="">

            <select name="database">

                <option value="DB1">DB1</option>

                <option value="DB2">DB2</option>

                <option value="DB3">DB3</option>

            </select>

        </form>



    <?php

    include ('queries.php');

    $test_1 = mysqli_query($mysqli_db, $test);

    echo "

                    <table>

                    <thead>

                        <tr>

                            <th>Column_1</th>

                        </tr>

                    </thead>";


    while ($row = mysqli_fetch_array($test_1)) {

        echo "<form method=\"post\"><tr>";

        echo "<td>" . $row['Column_1'] . "</td>";

        echo "</tr></form>";

    }

    echo "</table><br>";

    mysqli_close($mysqli_db);

    ?>

    </body>

    </html>

查询.php


<?php

$test = "select Column_1 from TEST; ";

?>

connection_db_1.php


<?php

// Connection data

$servername = "servename";

$username = "username";

$password = "pasword";

$dbname = "dbname_1";


// Create connection

$mysqli_db = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($mysqli_db->connect_error) {

    die("Connection failed: " . $mysqli_db->connect_error);

}

?>

connection_db_2.php


<?php

// Connection data

$servername = "servename";

$username = "username";

$password = "pasword";

$dbname = "dbname_2";


// Create connection

$mysqli_db = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($mysqli_db->connect_error) {

    die("Connection failed: " . $mysqli_db->connect_error);

}

?>

我认为,我需要一些 javascript/ajax 解决方案,但我不知道如何有效地使用它。


当年话下
浏览 99回答 2
2回答

红糖糍粑

现在,我看不到您展示任何使用或已实现任何 ajax 处理程序的示例。我可以建议您使用纯 Javascript 或 jQuery(通过包含 javascript 库,请参阅她在考虑实施 AJAX 之前,您可以做以下事情(它将包括第一次选择和表单提交后的数据库):<?phpif (isset($_POST["database"])) {&nbsp; &nbsp; $db = $_POST["database"];}?><!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="utf-8"/></head><?phpinclude_once("connection_db_" . $db . ".php"); /* This will include the selected connection */?><body><form action="" method="post">&nbsp; &nbsp; <select name="database">&nbsp; &nbsp; &nbsp; &nbsp; <option value="1">DB1</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="2">DB2</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="3">DB3</option>&nbsp; &nbsp; </select></form><?phpinclude('queries.php');$test_1 = mysqli_query($mysqli_db, $test);echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Column_1</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>";while ($row = mysqli_fetch_array($test_1)) {&nbsp; &nbsp; echo "<form method=\"post\"><tr>";&nbsp; &nbsp; echo "<td>" . $row['Column_1'] . "</td>";&nbsp; &nbsp; echo "</tr></form>";}echo "</table><br>";mysqli_close($mysqli_db);?></body></html>

aluckdog

数据库是否在同一台服务器上?如果是这样,您只需要一个与服务器的连接。您可以通过在查询中添加数据库名称来进行查询:select&nbsp;Column_1&nbsp;from&nbsp;dbname_1.TEST或者select&nbsp;Column_1&nbsp;from&nbsp;dbname_2.TEST
随时随地看视频慕课网APP
我要回答