Jump to content

[SOLVED] multiple selects in 1 (browser) page


bschultz

Recommended Posts

I'm trying to implement a weather cancellation script.  A user can enter in a cancellation (school, business, meeting, sports event) because of the weather, and it will display on a main page.

 

The script is working to a certain extent.  There are several categories that I'm displaying on the page.  As long as there are records that match, it works just fine.  If one select statement returns "false" it halts the remainder of the script.  So, in the code below, if the first select returns false, the second statement won't run.  Can someone tell me where my code is wrong?  Thanks.

 

<p><strong>Schools Closed Today</strong><br>
  <?php

$conn1 = mysql_connect("localhost", "username", "password");

if (!$conn1) {
   echo "Unable to connect to DB: " . mysql_error();
   exit;
}

if (!mysql_select_db("cancellations")) {
   echo "Unable to select mydbname: " . mysql_error();
   exit;
}
$sql1 = "SELECT date, type, event, action, comments FROM cancellations WHERE type = 'school' AND action = 'closed' AND date = CURDATE() ";

$result1 = mysql_query($sql1);

if (!$result1) {
   echo "Could not successfully run query ($sql1) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result1) == 0) {
   
echo "</strong>There aren't any schools closed for today!";
   exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop


while ($row1 = mysql_fetch_assoc($result1)) {

   echo $row1["event"];
   echo "  ";
   echo $row1["comments"];
   echo "<br>";
}

mysql_free_result($result1);

?>
</p>
<p><strong>Schools Running Two Hours Late Today</strong><br>
  <?php

$conn2 = mysql_connect("localhost", "username", "password");

if (!$conn2) {
   echo "Unable to connect to DB: " . mysql_error();
   exit;
}

if (!mysql_select_db("cancellations")) {
   echo "Unable to select mydbname: " . mysql_error();
   exit;
}
$sql2 = "SELECT date, type, event, action, comments FROM cancellations WHERE type = 'school' AND action = '2 Hours Late' AND date = CURDATE() ";

$result2 = mysql_query($sql2);

if (!$result2) {
   echo "Could not successfully run query ($sql2) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result2) == 0) {
   
echo "</strong>There aren't any schools running 2 hours late today!";
   exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop


while ($row2 = mysql_fetch_assoc($result2)) {

   echo $row2["event"];
   echo "  ";
   echo $row2["comments"];
   echo "<br>";
}

mysql_free_result($result2);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.