Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Easy selection of multiple SQL conditions from PHP

Are you automating an SQL query in a web page, wanting to select records to display only where they match a whole series of criteria from what could be a long list of options - for example looking for houses:

* Only in Melksham
* Less that 200k
* With a large garden

and other criteria that perhaps are required by other users might include:

* Near a good school
* Near public transport
* Ground floor
* Parking

If all that data is in your database - good, you can do it. But how do you build up the complex query? One useful trick is to have an array of possible queries, then push all the ones that you actually want into a further array and link them together with an implode putting an " and" between each element ..

$selectors = array(
  "cheap" => "asking < 200000",
  "local" => 'locate = "Melksham"',
  "bathphone" => 'phone like "01225%"');
$cwant = array();
foreach (array_keys($selectors) as $cdi) {
  if ($_GET[$cdi]) {
    array_push($cwant, $selectors[$cdi]);
  }
}
$restrict = implode (" and ",$cwant);


Demo here
Full source code here
(written 2007-11-30)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
S156 - Interfacing Applications to MySQL Databases
  [4436] Accessing a MySQL database from Python with mysql.connector - (2015-02-21)
  [3455] MySQL, MySQLi, PDO or something else - how best to talk to databases from PHP - (2011-09-24)
  [3447] Needle in a haystack - finding the web server overload - (2011-09-18)
  [3099] Perl - database access - DBD, DBI and DBIx modules - (2010-12-22)
  [3035] How to display information from a database within a web page - (2010-11-07)
  [2790] Joining a MySQL table from within a Python program - (2010-06-02)
  [2745] Connecting Python to sqlite and MySQL databases - (2010-04-28)
  [2381] Checking the database connection manually - (2009-08-28)
  [2263] Mysqldump fails as a cron job - a work around - (2009-06-30)
  [1885] Hiding a MySQL database behind a web page - (2008-11-15)
  [1561] Uploading to a MySQL database through PHP - examples and common questions - (2008-03-02)
  [1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
  [1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
  [723] Viewing images held in a MySQL database via PHP - (2006-05-17)
  [663] Python to MySQL - (2006-03-31)
  [644] Using a MySQL database from Perl - (2006-03-13)
  [104] mysql_connect or mysql_pconnect in PHP? - (2004-10-30)


Back to
Upgrade Mac OSX to Leopard, Web Server Apache httpd config lost
Previous and next
or
Horse's mouth home
Forward to
More PHP sample and demonstration programs
Some other Articles
From Sunday to Monday, from Melksham to the Balkans
What makes our courses special?
Santa on the train to Melksham
More PHP sample and demonstration programs
Easy selection of multiple SQL conditions from PHP
Upgrade Mac OSX to Leopard, Web Server Apache httpd config lost
Question on division (Java) - Also Perl, PHP, Python ...
Refa Tandoori Indian Restaurant, Melksham
An answer to a student asking 'Help'
Looking after you Christmas Customer Crowds
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1450_Eas ... m-PHP.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat