Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two parts to your question; 1 - Detecting which query to run and 2 - Displaying the results dynamically.</p> <p><strong>Part 1: Detecting which query to run:</strong></p> <p>Given hard-coded choices and no parameters for the query, using your above code, you can determine which query to run using the following:</p> <p>For the HTML part, as part of a form, create the select as you did above (but with a name)</p> <pre><code>&lt;select name="querySelect"&gt; &lt;option value="a"&gt;A&lt;/option&gt; &lt;option value="b"&gt;B&lt;/option&gt; &lt;/select&gt; </code></pre> <p>And in the PHP:</p> <pre><code>$querySelect = $_GET['querySelect']; switch($querySelect) { case 'a': $sql = "SELECT * FROM TableA"; break; case 'b': $sql = "SELECT * FROM TableB"; break; } $results = mysql_query($sql); </code></pre> <p><strong>Part 2: Displaying the results dynamically</strong></p> <p>With the <code>$results</code>, what you do with the data very much depends on what you want to achieve. At a very basic level, you can do the following to dynamically display a table of the results:</p> <pre><code>if(mysql_num_rows($results) &gt; 0) { $header = false; print "&lt;table&gt;" while($row = mysql_fetch_assoc($results)) { if(!$header) { $headings = array_keys($row); print "&lt;tr&gt;"; for($i=0;$i&lt;count($headings);$i++) { print "&lt;th&gt;".htmlspecialchars($headings[$i])."&lt;/th&gt;"; } print "&lt;/tr&gt;"; $header = true; } print "&lt;tr&gt;"; foreach($row as $value) { print "&lt;td&gt;".htmlspecialchars($value)."&lt;/td&gt;"; } print "&lt;/tr&gt;"; } print "&lt;/table&gt;" } else print "&lt;h1&gt;No Results Found!&lt;/h1&gt;"; mysql_free_result($results); </code></pre> <p>There is still alot not covered in my answer because I can't say what level of detail is required. You will also need to cover things like your connection to MySQL, error handling, formatting of the table...</p> <p><strong>Update</strong> Hmmm, very interested to know why this has been downvoted. If someone can please explain in comments where I have misinterpreted the question or misguided the user, I would appreciate it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload