elPato
elPato

Reputation: 35

No results from mysql_query

I am coding some PHP working with two MySQL databases. What I am working toward is different information to be sourced from the two databases which will then populate some form fields like the drop-down menu. The form will then be posted to create a printable document yada yada...

What Works

The connection to the first database works fine, the field is populated and there are no errors.

What Doesn't Work

When I introduce the second Database I get no errors but the form wont populate. I make this change...

From One Database:

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

To Two Databases:

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

The Connection

Source: 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);  
?> 

The Execution

It appears that $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn); Is my problem

<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>

Thanks In advance for any help :)

Upvotes: 2

Views: 2158

Answers (1)

airyt
airyt

Reputation: 352

you've got 2 mysql query commands going...

<?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)

should be

<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>

good luck!

Upvotes: 2

Related Questions