$data1=$_REQUEST['data1'];
$data2=$_REQUEST['data2'];
$data3=$_REQUEST['data3'];
mysqli_query($conn,"SELECT pu.id, monthname(pu.date_complete) AS Month
FROM `pds_user` pu
WHERE monthname(pu.date_complete) IN ('".implode("','",$data1)."')
AND pu.date_complete IN ('".implode("','",$data2)."')
AND Name IN ('".implode("','",$data3)."')
ORDER BY Month");
If $data1 is empty, what to do?
Use !empty then create dynamic where clause
$where_clause ='where 1=1';
if(!empty($data1))
{
$where_clause .= " And monthname(pu.date_complete) IN ('".implode("','",$data1)."')";
}
if(!empty($data2))
{
$where_clause .= " And pu.date_complete IN ('".implode("','",$data2)."') ";
}
if(!empty($data3))
{
$where_clause .= " And Name IN ('".implode("','",$data3)."')";
}
mysqli_query($conn,"SELECT pu.id, monthname(pu.date_complete) AS Month
FROM `pds_user` pu $where_clause ORDER BY Month");
Related
In the following example there is a base query. Other parameters can be dynamically added to complete the query.
However, my base query has no clause WHERE.
What is the best way to deal with it.
If I use in the base query, for example, WHERE 1 = 1, it seems to solve, but I have some doubts that is a correct solution.
$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
FROM fruits fr
LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";
if(!empty($countrys){
$myQuery .= " AND countrys = ? ";
}
if(!empty($sellers){
$myQuery .= " AND seller = ? ";
}
$myQuery .=" GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";
Edited: I fixed a writing gap from $empty to empty.
The WHERE 1=1 is a simplistic hack that works well because it simplifies your code. There is a great post here which explains the performance implications of WHERE 1=1. The general consensus is it will have no effect on performance.
Also, slight note ($empty) is probably not a function you've defined. I think you want empty(). You could write it like this:
$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
FROM fruits fr
LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";
$where = [];
if(!empty($countrys){
$where[] = "countrys = ?";
}
if(!empty($sellers){
$where[] = "seller = ?";
}
if (!empty($where)) {
$myQuery .= " WHERE " . implode(" AND ", $where);
}
$myQuery .= " GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";
You can use an array to control your SQL like this:
$where = [];
if(!$empty($countrys){
$where[] = " countrys = ? ";
}
if(!$empty($sellers){
$where[] = " seller = ? ";
}
if(count($where) > 0) {
$myQuery .= " WHERE ".implode('AND', $where);
}
I'm trying to filter through my database according to filters done by visitors.
$query = "select * from Sheet1 where";
//filter query start
if (!empty($brand)) {
$branddata = implode("','", $brand);
//testing removing query
$query .= " Brand in('$branddata') and";
}
if (!empty($model)) {
$modeldata = implode("','", $model);
//testing removing query
$query .= " Model in('$modeldata') and";
}
/* if(!empty($model) && empty($brand)){
} */
if (!empty($spower) && !empty($epower)) {
$query .= " Power>='$spower' and Power<='$epower' and";
}
if (!empty($sprice) && !empty($eprice)) {
$query .= " Doors>='$sprice' and Doors<='$eprice'";
}
$rs = mysqli_query($conn, $query) or die("Error : " . mysqli_error($conn));
The result I wish to get is a sql query that works and has correct syntax. Such as select * from Sheet1 where Doors>='$sprice' and Doors<='$eprice', if the visitor is filtering by price.
Currently, my code is made so that it simply adds a certain string to the variable. This means that if you don't filter by model, it skips model, because the model variable is empty. The problem comes to if you filter by power, the SQL will become select * from Sheet1 where Power>='$spower' and Power<='$epower' and. Obviously this doesn't work, so I need help in making the code make sure it works for every combination of filters.
Append $query .= " 1 = 1"; at the end. I did some modification in your given code. Have a look.
<?php
$query = "SELECT * FROM `Sheet1` WHERE";
//filter query start
if(!empty($brand)){
$branddata = implode("','",$brand);
$query .= " (Brand in('$branddata')) AND";
}
if(!empty($model)){
$modeldata = implode("','",$model);
$query .= " (Model in('$modeldata')) AND";
}
if(!empty($spower) && !empty($epower)){
$query .= " (Power>='$spower' AND Power<='$epower') AND";
}
if(!empty($sprice) && !empty($eprice)){
$query .= " (Doors>='$sprice' AND Doors<='$eprice') AND"; //Added 'AND'
}
$query .= " 1 = 1"; //Added new line
$rs = mysqli_query($conn,$query) or die("Error : ".mysqli_error($conn));
?>
Add AND on each query appended in if conditions. Then, at last add $query .= " 1 = 1";. Which will save you from extra AND coming at the end. If none of the conditions satisfy, then your query will be SELECT * FROM Sheet1 WHERE 1 = 1. Simple. And, don't forget to differentiate between conditions in query. Differentiate your conditions like how I did by opening and closing brackets.
I would do it this way
$filters=array();
if(!empty($brand)){
$branddata =implode("','",$brand);
//testing removing query
$filters[]= " Brand in('$branddata')";
}
if(!empty($model)){
$modeldata =implode("','",$model);
//testing removing query
$filters[]= " Model in('$modeldata') and";
}
if(!empty($spower) && !empty($epower)){
$filters[]= " Power>='$spower' and Power<='$epower' and";
}
if(!empty($sprice) && !empty($eprice)){
$filters[]= " Doors>='$sprice' and Doors<='$eprice'";
}
$query = "select * from Sheet1 where";
foreach ($filters as $filter) {
$query.=' AND '.$filter;
}
$rs = mysqli_query($conn,$query) or die("Error : ".mysqli_error($conn));
In mysql db table contact_details I have almost 12,000 rows and it's continuously updating.
Now I have a search form where I need to search data from the db table contact_details
For e.g : I am searching 2 in type column from contact_details table and there are almost 11,000 records of 2.
In this situation, my sql query is taking long time to produce result ! Sometime it's showing me Maximum time exceed. What should I do to get the result more quickly ?
Here is the contact_details table look like :
Here is the search form look like with error message :
I am using following sql query to get the search result :
if(!empty($ad_keyword)) {
$getSearch = "SELECT * FROM (SELECT GROUP_CONCAT(DISTINCT keywordName ORDER BY keywordName) as keywordName, ";
}
else{
$getSearch = "SELECT ";
}
$getSearch .= " cd.cdid, cd.family_name, cd.given_name, cd.department, cd.title, company.*, users.nickname, contact_label.label_data FROM
contact_details as cd
LEFT JOIN users ON users.user_id = cd.user_id
LEFT JOIN company ON company.cid = cd.cid
LEFT JOIN contact_docs ON contact_docs.cdid = cd.cdid
LEFT JOIN userkeywords ON userkeywords . cdid = cd . cdid
LEFT JOIN keywords ON keywords . kid = userkeywords . kid
LEFT JOIN contact_label ON contact_label.cdid = cd.cdid
WHERE 1=1 ";
if(!empty($ad_company)){
$getSearch .= "AND company.company_name LIKE '$ad_company%' ";
}
if(!empty($ad_fname)){
$getSearch .= "AND cd.family_name LIKE '$ad_fname%' ";
}
if(!empty($ad_department)){
$getSearch .= "AND cd.department LIKE '$ad_department%' ";
}
if(!empty($ad_mp)){
$getSearch .= "AND cd.mp >= '$ad_mp' ";
}
if(!empty($ad_e2)){
$getSearch .= "AND cd.e2 >= '$ad_e2' ";
}
if(!empty($ad_pl)){
$getSearch .= "AND cd.pl >= '$ad_pl' ";
}
if(!empty($ad_ap)){
$getSearch .= "AND cd.ap >= '$ad_ap' ";
}
if(!empty($ad_j2)){
$getSearch .= "AND cd.j2 >= '$ad_j2' ";
}
if(!empty($ad_agreater)){
$getSearch .= "AND cd.age >= '$ad_agreater' ";
}
if(!empty($ad_aless)){
$getSearch .= "AND cd.age <= '$ad_aless' ";
}
if(!empty($ad_agreater) && !empty($ad_aless)){
$getSearch .= "AND cd.age BETWEEN '$ad_agreater' AND '$ad_aless'";
}
if(!empty($ad_sgreater)){
$getSearch .= "AND cd.comp >= '$ad_sgreater' ";
}
if(!empty($ad_sless)){
$getSearch .= "AND cd.comp <= '$ad_sless' ";
}
if(!empty($ad_sgreater) && !empty($ad_sless)){
$getSearch .= "AND cd.comp BETWEEN '$ad_sgreater' AND '$ad_sless'";
}
if(!empty($ad_noteterm)){
$ad_noteterm = preg_replace("/\{ASUSIBBIR\}(.+?)\s:\s(.+?)\{ASUSIBBIR\}/m", "$2", $ad_noteterm);
$getSearch .= "AND LOCATE('$ad_noteterm', REPLACE (notesUpdate, '{ASUSIBBIR}', ' '))";
}
if(!empty($ad_cnote)){
$getSearch .= "AND LOCATE('$ad_cnote', cd.characterNotes)";
}
if(!empty($ad_twork)){
$getSearch .= "AND contact_label.label_data LIKE '%$ad_twork%'";
}
if(!empty($ad_tmobile)){
$getSearch .= "AND cd.mobile_phone like '%$ad_tmobile%'";
}
if(!empty($ad_resume)){
$getSearch .= "AND LOCATE('$ad_resume', contact_docs.file_content)"; //is this the resume? yes
}
if(!empty($ad_datefrom) && empty($ad_dateto)){
$getSearch .= "AND cd.created_date BETWEEN '$ad_datefrom'AND '$date'";
}
if(!empty($ad_dateto) && empty($ad_datefrom)){
$getSearch .= "AND cd.created_date BETWEEN date('0000-00-00') AND '$ad_dateto' ";
}
if(!empty($ad_datefrom) && !empty($ad_dateto)){
$getSearch .= "AND cd.created_date BETWEEN '$ad_datefrom' AND '$ad_dateto'";
}
if(!empty($ad_type)){
$getSearch .= "AND cd.type = '$ad_type' ";
}
if(!empty($ad_wemail)){
$getSearch .= "AND cd.email LIKE '$ad_wemail%'";
}
if(!empty($ad_pemail)){
$getSearch .= "AND cd.email_private LIKE '$ad_pemail%'";
}
if(!empty($ad_title)){
$getSearch .= "AND cd.title LIKE '$ad_title%'";
}
if(!empty($ad_source)){
$getSearch .= "AND cd.source LIKE '$ad_source%'";
}
if(!empty($ad_consultant)){
$getSearch .= "AND users.nickname LIKE '%$ad_consultant%'";
}
if(!empty($ad_keyword)){
$ad_keyword_param = str_replace(",","','",$ad_keyword);
$getSearch .= " AND keywords.keywordName IN ('$ad_keyword_param') ";
}
$getSearch .= " GROUP BY cd.user_id, cd.cid, cd.cdid ";
if(!empty($ad_keyword)){
$ad_keyword_param = str_replace(",",",",$ad_keyword);
$getSearch .= " ) as a WHERE keywordName LIKE '$ad_keyword_param%' ";
}
Implement indexing
Instead of fetch '*' specify only the required column name.
instead of subquery try to use join
use 'limit' clause
First You need to do indexing with your all table like users,company etc...
about that error solving of fatal error please put below line in php script in first line
ini_set('max_execution_time', 0);
I'm having a little trouble with a search form I've been creating the functionality for. I basically want a form (on whatever page) to go to this page then list the relevant rows from my database. My problem is that the form has both a text field and a select field (for name and categories) and I've been unable to create the functionality for having these two values search the database together.
So heres what I want to happen: When you only type in the name and not the category, it will display from just the name, vise versa for the category and no name; then when both together it only displays rows with both of them in.
Heres what I have so far:
// 2. Create variables to store values
if(!$_GET['search-category'] == "") {
$searchName = $_GET['search-name'];
}
if(!$_GET['search-category'] == "select-your-category") {
$searchCat = $_GET['search-category'];
}
// 2. Create the query for the stored value. Matching it against the name, summary and sub type of my item.
$mainSearch = "SELECT attraction.*, type.type_name, sub_type.sub_type_name ";
$mainSearch .= "FROM attraction ";
$mainSearch .= "INNER JOIN sub_type ON attraction.sub_type = sub_type.sub_type_id ";
$mainSearch .= "INNER JOIN type ON attraction.type = type.type_id ";
$mainSearch .= "WHERE attraction.name LIKE '%" . $searchName . "%' AND (sub_type.sub_type_name LIKE '%" . $searchCat . "%' )";
$mainSearch .= "ORDER BY sub_type_name ASC";
// 2. run query
$result2 = $con->query($mainSearch);
if (!$result2) {
die('Query error: ' . mysqli_error($result2));
}
I'd refactor the code to something like -
foreach( $_GET['filters'] as $fname => $fval ) {
if( !$fval ) continue;
$where[] = "$fname LIKE '%{$fval}%'";
}
You need to include only those inputs that are non-empty in the query. Also you will need to address security issues like escaping inputs etc.
What you can do is that declare a variable called $search_condition and based on whether $searchName or $searchCat is null or not assign value to $search_condition
For e.g.
if (isset($searchName ) || !is_empty($searchName ))
{
$search_condition = "WHERE attraction.name LIKE '%" . $searchName;
}
if (isset($searchCat ) || !is_empty($searchCat ))
{
$search_condition = "sub_type.sub_type_name LIKE '%" . $searchCat . "%'";
}
if ((isset($searchName ) || !is_empty($searchName )) && (isset($searchCat ) || !is_empty($searchCat )))
{
$search_condition = "WHERE attraction.name LIKE '%" . $searchName . "%' AND (sub_type.sub_type_name LIKE '%" . $searchCat . "%' )";
}
Hope this might help you
Thanks
This is a comment, but I want to take advantage of formatting options...
You know, you can rewrite that this way...
// 2. Create the query for the stored value. Matching it against the name, summary and sub type of my item.
$mainSearch = "
SELECT a.*
, t.type_name
, s.sub_type_name
FROM attraction a
JOIN sub_type s
ON a.sub_type = s.sub_type_id
JOIN type t
ON a.type = t.type_id
WHERE a.name LIKE '%$searchName%'
AND s.sub_type_name LIKE '%$searchCat%'
ORDER
BY s.sub_type_name ASC;
";
You can just check that the relevant values aren't empty:
// 2. Create the query for the stored value.
// Matching it against the name, summary and sub type of my item.
$mainSearch = "SELECT attraction.*, type.type_name, sub_type.sub_type_name ";
$mainSearch .= "FROM attraction ";
$mainSearch .= "INNER JOIN sub_type ON attraction.sub_type = sub_type.sub_type_id ";
$mainSearch .= "INNER JOIN type ON attraction.type = type.type_id ";
$mainSearch .= "WHERE ";
if ($searchName) {
$mainSearch .= "attraction.name LIKE '%" . $searchName . "%'";
if ($searchCat) {
$mainSearch .= " AND ";
}
}
if ($searchCat) {
$mainSearch .= "sub_type.sub_type_name LIKE '%" . $searchCat . "%'"
}
$mainSearch .= "ORDER BY sub_type_name ASC";
// Double check that at least one of the search criteria is filled:
if (!$searchName && !$searchCat) {
die("Must supply either name search or category search");
}
I'm using Mysql 5.5.16. I've a query where i combine 3 queries using union opeartor... Each query contains more than 10000 records.
My Query looks like...
$search_qry1 = " (SELECT PC_name as name FROM profile_category_tbl WHERE PC_status=1 and PC_parentid!=0 and (";
$search_qry2 = " (SELECT PROFKEY_name as name FROM profile_keywords_tbl WHERE PROFKEY_status=1 and (";
$search_qry3 = " (SELECT COM_name as name FROM ".$C."_company_profile_tbl WHERE COM_status=1 and (";
$order_by = "ORDER BY";
$word_length = explode(" ", $my_data);
for($i=0; $i <= count($word_length) - 1; $i++) {
$dt = $word_length[$i];
if ($dt) {
if ($i == 0) {
$or="";
}
else {
$or="OR";
}
$search_qry1 .= " $or regex_replace('[^a-zA-Z0-9\-]','',remove_specialCharacter(PC_name)) LIKE '%$dt%' ";
$search_qry2 .= " $or regex_replace('[^a-zA-Z0-9\-]','',remove_specialCharacter(PROFKEY_name)) LIKE '%$dt%' ";
$search_qry3 .= " $or regex_replace('[^a-zA-Z0-9\-]','',remove_specialCharacter(COM_name)) LIKE '%$dt%' ";
$order_by .= " IF(name LIKE '%$dt%',1,0) ";
}
if ($i == count($word_length) - 1) {
$search_qry1 .= ")";
$search_qry2 .= ")";
$search_qry3 .= ")";
}
if ($i != count($word_length) - 1) {
$order_by .= "+ ";
}
}
$search_qry1 .= " GROUP BY PC_name )";
$search_qry2 .= " GROUP BY PROFKEY_name )";
$search_qry3 .= " GROUP BY COM_name )";
$search_qry = "select name from ( $search_qry1 UNION ALL $search_qry2 UNION ALL $search_qry3 )a $order_by desc LIMIT 0,25";
It works perfectly... but it takes the time to execute it - more than 4 secs... for each search.... how possible to reduce its execution time?.... If anyone know an idea about this please let me know....
Actually I run this query for auto-complete search. If type "Rest" in search box, then
The Output Should be,
Family Restaurants
Everest Park
Fast Food Restaurants
Everest Park Residency
Fish Restaurant
Everest Power Solution
Fish Restaurants
Thanks in Advance,
Jeni
You are using regex_replace and like "%foobar%",
that actually slows down your query significat, since mySql has to do a fulltext search and the regex operation on every single row. Maybe it's faster if you do these operations after you fetched your results, using php instead of mysql.
The main problem is that you're doing a full scan of each table to check the like '%$dt%' condition. No matter which indices you have on the table, none of them will be used.
But if you post the final SQL sentence and what are you trying to do maybe we could help.