How to write a query for PHP and MySQL?

how to write php MySql query

  • I would like to be able to make a selection from a select-box and view the selected table. My skill level with php/MySQL at this point is: $query = "SELECT * FROM electrical ORDER BY item_id LIMIT $offset, $RPP"; With this query the div is populated on page load. I have a select box that has 6 choices: <form> <select name="dbtables" onchange="showTable(this.value)" id="selectBox"> <option value="">Select a materials table:</option> <option value="" id="hr"></option> <option value="equipment">Worker Equipment</option> <option value="tool">General Tools</option> <option value="electrical">Electrical Materials</option> <option value="mechanical">Mechanical Materials</option> <option value="plumbing">Plumbing Materials</option> <option value="hvac">HVAC Materials</option> </select> </form> Each choice is a table in a single MySQL database. Script for showTable is working, after making selection alert shows choice picked. function showTable(str) { if (str == "") { document.getElementById("materials").innerHTML = ""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("materials").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "index.php?q=" + str, true); xmlhttp.send(); alert(str); } Any pointers or help is appreciated appreciated.

  • Answer:

    in your index.php pick the $str value using $_GET method and construct the query. then run that query and echo the data. after that, you can call this function in the script file after alert(). function useHttpResponse() { if (http.readyState == 4) { if(http.status == 200) { var txt = http.responseText; var response_msg="<h4><span class='darkblue'>" + txt + "</span></h4>" document.getElementById('showtime').innerHTML = response_msg; } } else { //you may skip this else portion document.getElementById('showtime').innerHTML = '<img src="images/loading.gif" /><span class="darkblue">retrieving data...</span>'; } } here your fetched data will show on a div with id "showtime". during the fetching the time a loading gif will be visible in the "showtime" div. or you may skip that else portion. hope that function will help you.

ussteele at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I think what you're looking for is the SQL-query to execute on your database. What you should do is to insert the q-variable from the query string into the SQL-query like this $query = "SELECT * FROM " . $_REQUEST['q'] . " ORDER BY item_id LIMIT $offset, $RPP"; Ofcourse you should check or escape the variables to prevent SQL injections, using something like mysql_real_escape_string, but that's not really the scope of this question.

Flygenring

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.