2

我正在编写一些使用两个 MySQL 数据库的 PHP。我正在努力的是从两个数据库中获取不同的信息,然后这些信息将填充一些表单字段,例如下拉菜单。然后将发布该表格以创建可打印的文档 yada yada...

什么有效

与第一个数据库的连接工作正常,该字段已填充并且没有错误。

什么不起作用

当我介绍第二个数据库时,我没有收到任何错误,但不会填充表单。我做这个改变...

从一个数据库:

$sql = mysql_query"SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC";

到两个数据库:

$sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);

连接

资料来源:http ://rosstanner.co.uk/2012/01/php-tutorial-connect-multiple-databases-php-mysql/

如何在一个网页上连接多个 MySQL 数据库?

<?php  
// connect to the database server  
$conn = mysql_connect("localhost", "cars", "password");  

// select the database to connect to  
mysql_select_db("manufacturer", $conn);  

// connect to the second database server  
$conn2 = mysql_connect("localhost", "cars", "password");  

// select the database to connect to  
mysql_select_db("intranet", $conn2);  
?> 

执行

看来 $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn); 是我的问题

<form name="form" method="post" action="review.php">
<table><td>
    <select>
    <option value="">--Select--</option>
<?php $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);  
      $rs_result = mysql_query ($sql); 

// get the entry from the result
   while ($row = mysql_fetch_assoc($rs_result)) {

// Print out the contents of each row into a table 
   echo "<option value=\"".$row['carname']."\">".$row['carname']."</option>";
    }
?>
    </select>
</td></table>
</form>

在此先感谢您的帮助:)

4

1 回答 1

2

你有 2 个 mysql 查询命令正在运行......

<?php
$sql       = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);  
$rs_result = mysql_query ($sql); // <-- $sql here is the result of the first query (ie. not a sql command)

应该

<form name="form" method="post" action="review.php">
<table><td>
    <select>
    <option value="">--Select--</option>
<?php
    $sql = "SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC";
    $rs_result = mysql_query( $sql, $conn );

    // get the entry from the result
    while ($row = mysql_fetch_assoc($rs_result)) {
        // Print out the contents of each row into a table 
        echo "<option value=\"".$row['carname']."\">".$row['carname']."</option>";
    }
?>
    </select>
</td></table>
</form>

祝你好运!

于 2012-11-16T00:50:49.940 回答