jamescover
07-24-2004, 12:43 PM
Hi:
Thanks for bearing with me, as I learn PHP and db queries. I did a search but didn't find anything directly addressing my question...
I want to make two queries with a single script, because I want to be able to output different results, in the event that there is no match for the first query, which searches for a variable passed from a select menu. But there is one option in the select menu that has no match in the db, so I want to return different results, which in this case, is all rows (All Models) for the given column (Models).
I have gotten this to work, but it doesn't seem intuitive. I tried using the if and else clauses with the actual queries, but without success. Would someone be willing to show me the proper way of going about this--how to script it--but produce the same results.
<?php
$db = mysql_connect ("localhost", "username", "password") or die (mysql_error());
mysql_select_db ("mydbname", $db) or die(mysql_error());
//$getModels is the name of my select, and all options but one match rows in the db
$models = mysql_query ("SELECT model FROM mytablename WHERE make = '$getModels' ORDER BY model ASC") or die (mysql_error());
//This query is used to return all models, when the "VIEW ALL MODELS" option is selected, since it doesn't match any row in the db
$allModels = mysql_query ("SELECT model FROM mytablename ORDER BY model ASC") or die (mysql_error());
if (mysql_num_rows ($models) != 0) {
echo "<form name=\"oForm2\" method=\"post\" action=\"getResults.php\">\n";
echo "<select name=\"getModels\">\n";
echo "<option value=\"**\" selected>Select a Model</option>\n";
while ($row = mysql_fetch_array ($models)) {
$r_model = $row[model];
echo "<option value=\"$r_model\">$r_model</option>\n";
}
echo "</select></form>";
} else if (mysql_num_rows ($allModels) != 0) {
echo "<form name=\"oForm2\" method=\"post\" action=\"getResults.php\">\n";
echo "<select name=\"getModels\">\n";
echo "<option value=\"**\" selected>Select a Model</option>\n";
while ($row = mysql_fetch_array ($allModels)) {
$r_model = $row[model];
echo "<option value=\"$r_model\">$r_model</option>\n";
}
echo "</select></form>";
} else {
echo "<form name=\"oForm2\">\n";
echo "<select name=\"getModels\">\n";
echo "<option value=\"**\" selected>Select a Model</option>\n";
echo "<option value=\"null\">No models in database</option>\n";
echo "</select></form>";
}
mysql_close($db);
?>
I've commented the above--hope it helps.
Below is a link to the working scripts (again, this works as desired):
http://www.ekigroup.com/dbdemo/testPage.php
Any help is appreciated. Thanks!
-james
Thanks for bearing with me, as I learn PHP and db queries. I did a search but didn't find anything directly addressing my question...
I want to make two queries with a single script, because I want to be able to output different results, in the event that there is no match for the first query, which searches for a variable passed from a select menu. But there is one option in the select menu that has no match in the db, so I want to return different results, which in this case, is all rows (All Models) for the given column (Models).
I have gotten this to work, but it doesn't seem intuitive. I tried using the if and else clauses with the actual queries, but without success. Would someone be willing to show me the proper way of going about this--how to script it--but produce the same results.
<?php
$db = mysql_connect ("localhost", "username", "password") or die (mysql_error());
mysql_select_db ("mydbname", $db) or die(mysql_error());
//$getModels is the name of my select, and all options but one match rows in the db
$models = mysql_query ("SELECT model FROM mytablename WHERE make = '$getModels' ORDER BY model ASC") or die (mysql_error());
//This query is used to return all models, when the "VIEW ALL MODELS" option is selected, since it doesn't match any row in the db
$allModels = mysql_query ("SELECT model FROM mytablename ORDER BY model ASC") or die (mysql_error());
if (mysql_num_rows ($models) != 0) {
echo "<form name=\"oForm2\" method=\"post\" action=\"getResults.php\">\n";
echo "<select name=\"getModels\">\n";
echo "<option value=\"**\" selected>Select a Model</option>\n";
while ($row = mysql_fetch_array ($models)) {
$r_model = $row[model];
echo "<option value=\"$r_model\">$r_model</option>\n";
}
echo "</select></form>";
} else if (mysql_num_rows ($allModels) != 0) {
echo "<form name=\"oForm2\" method=\"post\" action=\"getResults.php\">\n";
echo "<select name=\"getModels\">\n";
echo "<option value=\"**\" selected>Select a Model</option>\n";
while ($row = mysql_fetch_array ($allModels)) {
$r_model = $row[model];
echo "<option value=\"$r_model\">$r_model</option>\n";
}
echo "</select></form>";
} else {
echo "<form name=\"oForm2\">\n";
echo "<select name=\"getModels\">\n";
echo "<option value=\"**\" selected>Select a Model</option>\n";
echo "<option value=\"null\">No models in database</option>\n";
echo "</select></form>";
}
mysql_close($db);
?>
I've commented the above--hope it helps.
Below is a link to the working scripts (again, this works as desired):
http://www.ekigroup.com/dbdemo/testPage.php
Any help is appreciated. Thanks!
-james