Make a dynamic query to mysql from php - php

I want to create a page when a user can select like in a drop-down menu the fields to make a query in a simple mySql database, and then print on the screen the results.
Some suggestions?

If you have Microsoft Access, look at the way queries are generated as you select fields with the GUI (switch to SQL view to see the query).

Simply generate a HTML Form that lists the available options from the Database and then from the end user's input, Concatenate the input/strings together to make up a valid MySQL Query that would be returned.
But it's you who's got to do the actual coding ;)

Check my answer here ..Similar kind of question.Hope it help
PHP/MSSQL - Filtering from User Input (HTML)

You can do it like:
$get_dropdown_value=isset($_REQUEST["result"])?$_REQUEST["result"]:"";
//Query
mysql_query("SELECT * FROM $get_dropdown_value");
get the dropdown value from your code. and assign that in $get_dropdown_value.

Related

How GET/REQUEST all values from a field in a database (phpMyAdmin) using SQL and PHP?

I am currently doing work with a database online. The is entries from different countries and I need to do a drop down list for these countries. Rather than typing the countries into the HTML is there any way of using GET/REQUEST or anything to get the data from the database to create a drop down?
Obviously, there are a few duplicate countries such as I think USA occurs 3 or 4 times but I would like that to appear on the list only once.
Update: coming to think of it GET/REQUEST can't be used so is there an alternative possibly using an SQL statement?
Basic structure of an HTML dropdown is:
<select>
<option>Option1</option>
<option>Option2</option>
<option>Option3</option>
</select>
Knowing that, simple make a query from your database, and loop through the values. Assuming you already know how to do that.
<select>
<?php
while(<loop through each value in database>){
echo "<option>{$data}</option>"; //Assuming $data has current iteration's value
}
?>
</select>
the equivalent sql statement for GET is SELECT.

Search query containing a variable

HI I am working on a php mysql project and need some help.
On one of my fields I use a check box to enter a value. The possible options are 9001 14001 and 18001.
If I tick 14001 and 18001 the result that gets stored is 14001,18001.
When I set up a search I have had to set up an if equals for each possible combination. ..not too bad in this case as only 7.
Bit what would I use in an sql query if I wanted to say if (field) contains?
The following query would give you all line where field contains 14001.
SELECT * FROM table WHERE field LIKE '%14001%'
But you might want to reconsider the way you store your data to make it faster.
One way to do it is nicely described here:
http://www.phpknowhow.com/mysql/many-to-many-relationships/

MySQL - ListMenus/Comboboxes

I am trying to build a dynamic set of comboboxes/listMenus getting data from a MySQL DB. My database has 5 fields, 1st being id and
topic, sub_topic, info and url
I want to make it so that until user selects a valid choice from box1 that the others are disabled. Once user selects from box 1 box 2 will be activated. Once you make a selection from box 2 then the info and url will be shown.
I have followed a tutorial http://www.ssdtutorials.com/tutorials/series/dependable-dropdown.html and so most of this code is not mine apart from the SELECT statements
I am having problems writing the MySQL for update.php as this populates the other boxes (currently I am sticking with the comboboxes until I have it working.)
I would be grateful for some help, due to the amount of code it can be seen here http://pastebin.com/QNbHR9JK
Thanks in advance.
The first thing I see is that in update.php query, you aren't actually using a where clause. When you execute the prepared query, you pass in a value array, but there aren't any placeholders in the prepared query.
I would expect something like:
$sql = "SELECT `topic`,`sub_topic`,`info`,`url`
FROM `links` WHERE sub_topic=?";
$statement = $objDb->prepare($sql);
$statement->execute(array($value));
One other thing I notice is that in your foreach in update.php, you are using $row['id'] but you aren't selecting that column in your query.
EDIT
I updated the query to use the proper column in where clause, and removed to group by clause based on discussion.

Click a link to run MySQL query for that specific value in the link (php)

I had a hard time to find a solution for this, most of the questions asked were to click a button to run a query.
What I have is a list of db table names in a HTML table. I need them to be in hyperlink format, once I click a table name it should send the name of the table to a php file and run mysql query to get all the attributes of the table.
What's the best way to do this? thanks for all suggestions in advance.
huh? how about phpmyadmin if you want to use querying
but if you like to build your own SQL query tool then learn this things:
List database:
http://php.net/manual/en/function.mysql-list-dbs.php
List Tables:
http://www.php.net/manual/en/function.mysql-list-tables.php
and lastly List Fields
http://www.php.net/manual/en/function.mysql-list-fields.php
ADDING THE AMBIGUOUS REQUEST OF Q-Starter.
#example:index.php
<form method="get" action="query.php">
<label>Statement: </label><input type="text" name="sqlState"/>
</form>
#example:query.php
<?php
$sqlState = $_GET['sqlState'];
$result = mysql_query($sqlState);
echo $result;
?>
This is bad programming codes but if you really want to explore then go and try this codes, just use this on your own, this might damaged your DATABASE if other users tries to use the so called "SQL INJECTION".

how to select entries from database for particular fields entered in php

I have php page thats taking values for startdate, enddate, hotelname, city, price and type from the user.
Now i need to check my database for only the values that are entered by the user. Is that possible? For any entry that is left blank, the corresponding condition is not applied (e.g., if city is left blank, all cities are considered).
Like if the user enters only the hotelname then i need to select * from hotel where hname = hotelname.
If i go on checking for all the possible set of conditions then it will be to long like 63 combinations are possible!! so is there any way out? please help! Thanks.
You should write a method that loops through the input vars and looks for valid column names instead of handling each input individually. Then make sure you are using prepared statements in your SQL (mysqli, PDO).
To check for specific input vars, you should perform an
if (isset($_POST['var']) && !empty($_POST['var']))
If each field needs special attention or separate database tables, then you will most likely need to handle each field individually.
It ultimately depends on your needs. But in my experience there is always a way to do it pro-grammatically.
When you build your query in your code, only include the items that have values in them.
You ARE building your query dynamically aren't you?

Categories