deesto
21st May 08, 01:40 PM
Hello,
I'm trying to display the results of a MySQL query based on user input. I have two drop-downs that pull distinct values from columns in a table, and on submit, I'd like to display the results based on those pull-down values.
The first part, which gets the pull-down values from the table, seems to work fine:
<?
$db_host = 'localhost';
$db_user = 'myuser';
$db_passwd = 'mypass';
$db_name = 'mydatabase';
// drop-down form for selecting column values:
<form method='post' action="<?=$_SERVER['PHP_SELF']?>"'>
<table align='center'> <tr>
<td>Select a value from column one:
<select name="column1value">
<?
$col1list = mysql_query("select distinct mytablecolumnone from mytable order by mytablecolumnone")
or die("column one query failed");
while ($record_col1 = mysql_fetch_array($col1list))
{
printf(" <option>%s</option>\n",$record_col1['mytablecolumnone']);
}
?>
</select>
</td>
<td>Select a value from column two:
<select name="column2value">
<?
$col2list = mysql_query("select distinct mytablecolumntwo from mytable order by mytablecolumntwo")
or die("column two query failed");
while ($record_col2 = mysql_fetch_array($col2list))
{
printf(" <option>%s</option>\n",$record_col2['mytablecolumntwo']);
}
?>
</select>
</td>
<td><input type="submit" name="querysubmit" value="View your results"></td>
</tr>
</table>
</form>It's the part where the results are printed that I can't seem to get right:
<?
$num = mysql_numrows($result);
// build the query. start with conditions from submitted input:
if (isset($_POST['querysubmit'])) {
if ($FilterSet)
{
$result = mysql_query("select * from mytable
WHERE id > 0
$FilterOneQuery
$FilterTwoQuery
order by mytablecolumntwo,mytablecolumnone");
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];
}
// otherwise, use the base query, without conditions from imput:
else
{
$result = mysql_query("select * from mytable
order by mytablecolumnone,mytablecolumntwo");
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];
}
// get specified column value information from submitted input:
$FilterOne = $_GET['column1value'];
$FilterTwo = $_GET['column2value'];
// sanitize the input (not really needed, but...):
$FilterOne = sanitize_filter_input($FilterOne);
$FilterTwo = sanitize_filter_input($FilterTwo);
// add input values as conditions to the query:
if (!empty($FilterOne))
{
$FilterOneQuery = "AND mytablecolumnone =" . $FilterOne;
$FilterSet = TRUE;
}
if (!empty($FilterTwo))
{
$FilterTwoQuery = "AND mytablecolumntwo =" . $FilterTwo;
$FilterSet = TRUE;
}
// print results:
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];
print "Displaying $num records.<br/>";
print "<table width=200 border=1>\n";
// print table columns as headings:
for ($i = 0; $i < mysql_num_fields($result); $i++) {
print '<th>' . mysql_field_name($result, $i) . '</th>';
}
// print table rows:
while($getrow = mysql_fetch_array($result))
{
print "<tr>\n";
foreach ($getrow as $field)
print "\t<td>$field</td>\n";
print "</tr>\n";
}
print "</table>\n";
}
// close connection:
mysql_close(); ?>The pull-downs contain column values form the MySQL table, and there are no errors on submitting the form, but no results are returned.
If I add a 'print $result' after each of the two column conditions, and to the print table section at the end, I get: "Resource id #5". If I change these to an array instead, as they are now:
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];... it's the same as before: nothing is printed.
Any thoughts on what I'm doing wrong?Thanks.
I'm trying to display the results of a MySQL query based on user input. I have two drop-downs that pull distinct values from columns in a table, and on submit, I'd like to display the results based on those pull-down values.
The first part, which gets the pull-down values from the table, seems to work fine:
<?
$db_host = 'localhost';
$db_user = 'myuser';
$db_passwd = 'mypass';
$db_name = 'mydatabase';
// drop-down form for selecting column values:
<form method='post' action="<?=$_SERVER['PHP_SELF']?>"'>
<table align='center'> <tr>
<td>Select a value from column one:
<select name="column1value">
<?
$col1list = mysql_query("select distinct mytablecolumnone from mytable order by mytablecolumnone")
or die("column one query failed");
while ($record_col1 = mysql_fetch_array($col1list))
{
printf(" <option>%s</option>\n",$record_col1['mytablecolumnone']);
}
?>
</select>
</td>
<td>Select a value from column two:
<select name="column2value">
<?
$col2list = mysql_query("select distinct mytablecolumntwo from mytable order by mytablecolumntwo")
or die("column two query failed");
while ($record_col2 = mysql_fetch_array($col2list))
{
printf(" <option>%s</option>\n",$record_col2['mytablecolumntwo']);
}
?>
</select>
</td>
<td><input type="submit" name="querysubmit" value="View your results"></td>
</tr>
</table>
</form>It's the part where the results are printed that I can't seem to get right:
<?
$num = mysql_numrows($result);
// build the query. start with conditions from submitted input:
if (isset($_POST['querysubmit'])) {
if ($FilterSet)
{
$result = mysql_query("select * from mytable
WHERE id > 0
$FilterOneQuery
$FilterTwoQuery
order by mytablecolumntwo,mytablecolumnone");
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];
}
// otherwise, use the base query, without conditions from imput:
else
{
$result = mysql_query("select * from mytable
order by mytablecolumnone,mytablecolumntwo");
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];
}
// get specified column value information from submitted input:
$FilterOne = $_GET['column1value'];
$FilterTwo = $_GET['column2value'];
// sanitize the input (not really needed, but...):
$FilterOne = sanitize_filter_input($FilterOne);
$FilterTwo = sanitize_filter_input($FilterTwo);
// add input values as conditions to the query:
if (!empty($FilterOne))
{
$FilterOneQuery = "AND mytablecolumnone =" . $FilterOne;
$FilterSet = TRUE;
}
if (!empty($FilterTwo))
{
$FilterTwoQuery = "AND mytablecolumntwo =" . $FilterTwo;
$FilterSet = TRUE;
}
// print results:
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];
print "Displaying $num records.<br/>";
print "<table width=200 border=1>\n";
// print table columns as headings:
for ($i = 0; $i < mysql_num_fields($result); $i++) {
print '<th>' . mysql_field_name($result, $i) . '</th>';
}
// print table rows:
while($getrow = mysql_fetch_array($result))
{
print "<tr>\n";
foreach ($getrow as $field)
print "\t<td>$field</td>\n";
print "</tr>\n";
}
print "</table>\n";
}
// close connection:
mysql_close(); ?>The pull-downs contain column values form the MySQL table, and there are no errors on submitting the form, but no results are returned.
If I add a 'print $result' after each of the two column conditions, and to the print table section at the end, I get: "Resource id #5". If I change these to an array instead, as they are now:
$resultarray = mysql_fetch_row_array($result);
print $resultarray[0];... it's the same as before: nothing is printed.
Any thoughts on what I'm doing wrong?Thanks.