我正在使用两个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/
How do you connect to multiple MySQL databases on a single webpage?
<?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(&#34; SELECT * FROM car WHERE color =&#39; blue&#39; ORDER BY sqm ASC&#34;,$ 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>
在此先感谢您的任何帮助:)
答案 0 :(得分: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>
祝你好运!