2

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

Community
  • 1
  • 1
elPato
  • 35
  • 9
  • 2
    Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [red box](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, here is [good PDO tutorial](http://goo.gl/vFWnC). – John Conde Nov 16 '12 at 00:38
  • 2
    What does `mysql_error()` tell you? – John Conde Nov 16 '12 at 00:38
  • Thanks John, the code is a few years old and I am making a small edit in the overall scheme. I wouldn't mind learning but I'm on holidays from next week so I probably wont get the changes done in time.. 'mysql_error()' tells me:Access denied for user 'cars'@'localhost' to database 'intranet' – elPato Nov 16 '12 at 00:47
  • Please add these to the end of form and tell us results ... var_dump($rs_result);echo mysql_error(); – Erdinç Çorbacı Nov 16 '12 at 00:49
  • 1
    Oh great we have deprecation theorists here. – Adam Fowler Nov 16 '12 at 00:49
  • `var_dump($rs_result);echo mysql_error();` replies with `bool(false) Query was empty` – elPato Nov 16 '12 at 00:53
  • @PatHarding - you've got – airyt Nov 16 '12 at 01:02

1 Answers1

2

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!

airyt
  • 354
  • 2
  • 9
  • I had some minor user errors to fix up but Airyt you were absolutely spot on! Thank you for your answer! – elPato Nov 16 '12 at 01:19