The first page, where they select the search fields and submit:
Expand|Select|Wrap|Line Numbers
- <?php
- $db = mysql_connect("localhost", "root", "yeahright");
- if (!$db)
- {
- die('Could not connect:'.mysql_error);
- }
- mysql_select_db("mytest",$db);
- $sql = "SELECT distinct fac_id FROM incidents ORDER BY fac_id";
- $result = mysql_query($sql);
- if (!$result)
- {
- die('Error: '.mysql_error());
- }
- $sql2 = "SELECT distinct person_type FROM incidents ORDER BY person_type";
- $result2 = mysql_query($sql2);
- if (!$result2)
- {
- die('Error: '.mysql_error());
- }
- $sql3 = "SELECT distinct severity FROM incidents ORDER BY severity";
- $result3 = mysql_query($sql3);
- if (!$result3)
- {
- die('Error: '.mysql_error());
- }
- mysql_close($db);
- ?>
Expand|Select|Wrap|Line Numbers
- <FORM METHOD="POST" ACTION="ir_multiquery.php">
- <p>Search existing Incident Reports by selecting one of the search fields below:</p>
- <select name="fac_id" size="1">
- <option selected value="">Facility</option>
Expand|Select|Wrap|Line Numbers
- <?php
- while ($myrow = mysql_fetch_array($result))
- {
- echo "<option value='fac_id'>".$myrow["fac_id"]."</option>\n";
- }
- ?>
Expand|Select|Wrap|Line Numbers
- </select>
- <select name="person_type" size="1">
- <option selected value="">Person Type</option>
Expand|Select|Wrap|Line Numbers
- <?php
- while ($myrow2 = mysql_fetch_array($result2))
- {
- echo "<option value='person_type'>".$myrow2["person_type"]."</option>\n";
- }
- ?>
Expand|Select|Wrap|Line Numbers
- </select>
- <select name="severity" size="1">
- <option selected value="">Severity Level</option>
Expand|Select|Wrap|Line Numbers
- <?php
- while ($myrow3 = mysql_fetch_array($result3))
- {
- echo "<option value='severity'>".$myrow3["severity"]."</option>\n";
- }
- ?>
Expand|Select|Wrap|Line Numbers
- </select>
- <p>
- <input type="submit" value="Search" />
- </p>
- </FORM>
Expand|Select|Wrap|Line Numbers
- <?php
- $db = mysql_connect("localhost", "root", "yeahright");
- if (!$db)
- {
- die('Could not connect:'.mysql_error);
- }
- mysql_query("mytest", $db);
- // The basic SELECT statement
- $select = 'SELECT fac_id';
- $from = ' FROM incidents';
- $where = ' WHERE 1=1';
- // If facility was selected
- $facility = $_REQUEST["fac_id"];
- if ($facility != '') {
- echo "REQUEST Facility: ";
- echo $_REQUEST["fac_id"];
- echo " ";
- $where .= ' AND fac_id='.$_REQUEST["fac_id"].'';
- //$where .= " AND fac_id='000955'";
- }
- // If person type was selected
- $persontype = $_REQUEST["person_type"];
- if ($persontype != '') {
- echo "Persontype: ";
- echo $_REQUEST["person_type"];
- $where .= ' AND person_type='.$_REQUEST["person_type"].'';
- }
- // If severity was selected
- $severity = $_REQUEST["severity"];
- if ($severity != '') {
- echo "Severity: ";
- $_REQUEST["severity"];
- $where .= ' AND severity='.$_REQUEST["severity"].'';
- }
- echo " Final Select: ";
- echo $select;
- echo $from;
- echo $where;
- ?>
Expand|Select|Wrap|Line Numbers
- <table>
- <tr><th>Query</th><th>Results</th></tr>
Expand|Select|Wrap|Line Numbers
- <?php
- $query = sprintf($select . $from . $where);
- echo 'query: ';
- echo $query;
- if (!$query)
- {
- die('Could not run query:'.mysql_error);
- }
- $result = mysql_query($query);
- echo '<p>result: ';
- echo $result;
- echo '</p>';
- if (!$result) {
- echo '</table>';
- exit('<p>Error retrieving data from db - this is from the $result var<br />'.
- 'Error: ' . mysql_error() . '</p>');
- }
- else
- {
- echo '<p>Eureka!</p>';
- }
- while ($myrow = mysql_fetch_array($result))
- {
- echo "<tr valign='top'>\n";
- $facility = $myrow['fac_id'];
- echo "<td>$facility</td>\n";
- echo "</tr>\n";
- }
- mysql_close($db);
- ?>
Expand|Select|Wrap|Line Numbers
- </table>
The echo of $query shows that the three values I am trying to pass do not get passed - for example, if I select Facility it shows
SELECT fac_id FROM jos_incidents_combined WHERE 1=1 AND fac_id=fac_id
This of course causes the $result to be blank.
I then get the error:
Error retrieving data from db - this is from the $result var
Error: No database selected
I have checked and rechecked my code for the db connection, and it is functioning. Also, when I paste the $query in phpmyadmin without the AND part, it runs successfully.
Is the problem just the fact that I'm not passing the values or does the 'No database selected' indicate a second problem as well?
What changes do I need to do to my code to get the variables passed?
Thanks - I apologize for the large content - but I have re-read some of the PHP materials and tried a few changes but nothing seems to work.
TIA,
jej1216