Limited number of SQL Query selectors - php

I can't figure out what is wrong with my query. I have three elements that control the order of posts: quartier, block and number. And it is working well.
I want to add a fourth named street. When I do so, nothing is fetched. I thought maybe there is a problem with street, but if for example I leave street but remove block, it is working again and uses street appropriately.
My conclusion is that the database only accepts a limited number of selectors per query. Is that true? Or am I missing something?
$query = "SELECT `wp_posts`.`post_title` , `wp_posts`.`ID` , `end`.`meta_value` AS `enddate` , `start`.`meta_value` as `startdate`, `quartier`.`meta_value` AS `thequartier`, `street`.`meta_value` AS `thestreet`, `number`.`meta_value` AS `thenumber`, `block`.`meta_value` AS `theblock`
FROM `wp_posts` , `wp_postmeta` AS `end` , `wp_postmeta` AS `start` , `wp_postmeta` AS `quartier`, `wp_postmeta` AS `street`, `wp_postmeta` AS `number`, `wp_postmeta` AS `block`
WHERE `wp_posts`.`post_type` = 'listing'
AND `wp_posts`.`ID` = `end`.`post_id`
AND `wp_posts`.`ID` = `start`.`post_id`
AND `wp_posts`.`ID` = `quartier`.`post_id`
AND `wp_posts`.`ID` = `street`.`post_id`
AND `wp_posts`.`ID` = `block`.`post_id`
AND `wp_posts`.`ID` = `number`.`post_id`
AND `end`.`meta_key` = 'theend'
AND `end`.`meta_value` >= '".time()."'
AND `start`.`meta_key` = 'thestart'
AND `start`.`meta_value` <= '".time()."'
AND `quartier`.`meta_key` = 'quartier'
AND `street`.`meta_key` = 'street'
AND `number`.`meta_key` = 'number'
AND `block`.`meta_key` = 'block'
AND `wp_posts`.`post_status` = 'publish'
GROUP BY `wp_posts`.`ID`
ORDER BY `thequartier` + 0 ASC";
$calendar = $wpdb->get_results($query, ARRAY_A);
foreach ($calendar as $key => $row) {
$thequartier[$key] = $row['thequartier'];
$thestreet[$key] = $row['thestreet'];
$theblock[$key] = $row['theblock'];
$thenumber[$key] = $row['thenumber'];
}
// Sort the data with volume descending, edition ascending
array_multisort($thequartier, SORT_ASC, $theblock, SORT_ASC, $thestreet, SORT_ASC, $thenumber, SORT_ASC, $calendar);
Update: I found the solution for my problem, adding the following line to allow the excessive amount of selectors:
$wpdb->query("SET SQL_BIG_SELECTS=1");

Related

I'm struggling with the WHERE IN query

I am not able to use this code. I've echoed the implode (and it works perfectly) but I cannot seem to get it to work with the WHERE IN query.
I've tried echo the import, which works, and the code works perfectly when I remove the WHERE IN, so I know the elements within 'etc etc' are working, too.
$filter_category = "SELECT * FROM `youcard_wp`.`bc_term_relationships`
WHERE term_taxonomy_id = '57'";
$result_c = $conn90->query($filter_category);
while($row_c = $result_c->fetch_assoc()){
$category_filter[] = $row_c['object_id'];
}
echo implode(',', $category_filter); // <- works great
$get_offer_list = "
SELECT * FROM `youcard_wp`.`bc_posts`
WHERE `ID` IN ('".implode(',', $category_filter)."')
AND post_status = 'publish'
AND post_type = 'portfolio'
ORDER BY menu_order ASC
";
$result = $conn90->query($get_offer_list);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
etc etc etc...
You don't need any of that. Instead use this...
SELECT x.columns
, x.you
, x.actually
, x.want
FROM bc_posts x
JOIN bc_term_relationships y
ON y.object_id = x.id
WHERE x.post_status = 'publish'
AND x.post_type = 'portfolio'
AND y.term_taxonomy_id = 57
ORDER
BY x.menu_order ASC
You are surrounding the whole IN list with a pair of single quotes, whereas you would need to quote each individual value.
Consider:
$get_offer_list = "
SELECT *
FROM `youcard_wp`.`bc_posts`
WHERE
`ID` IN ('" .implode("','", $category_filter)."')
AND post_status = 'publish'
AND post_type = 'portfolio'
ORDER BY menu_order ASC
";
If IDs are actually numeric values and not strings, then you do not need quotes at all:
$get_offer_list = "
SELECT *
FROM `youcard_wp`.`bc_posts`
WHERE
`ID` IN (" .implode(",", $category_filter).")
AND post_status = 'publish'
AND post_type = 'portfolio'
ORDER BY menu_order ASC
";

How to conditionally run PHP code only when query returns results?

I hope someone can help me with the best way to structure this code.
I run an oil club as a volunteer and if there are no quotes for a specific oil type, then the page fails due to empty variables being used in queries further down the code.
Ideally I would like to default to $win_supplier_red_id = 0 if the query returns no results, but I'm not sure the best way to catch it and where is the best place in the code.
$sql2a= "Select Quote_id from tbl_quote where (select min(quote_price) as best_red from tbl_quote where fuel_type_id =2 AND timestamp > date_sub( NOW(), INTERVAL 7 DAY ) AND quote_price > 10) = quote_price AND timestamp > date_sub( NOW(), INTERVAL 7 DAY ) Order by timestamp Limit 1";
$stmt2a = $db->prepare($sql2a);
$stmt2a->execute();
$res2a = $stmt2a->fetchObject();
$best_red_quote = $res2a->Quote_id;
$sql2= "SELECT qt.quote_id, ft.fuel_type_id, ft.fuel_name, st.supplier_id, st.company_name as company_name, st.email, qt.supplier_id, qt.timestamp, qt.fuel_type_id, min( qt.quote_price ) AS best_red
FROM tbl_quote qt
INNER JOIN `tbl_suppliers` st ON qt.supplier_id = st.supplier_id
INNER JOIN `tbl_fuel-type` ft ON qt.fuel_type_id = ft.fuel_type_id
WHERE qt.Quote_id = $best_red_quote
Order by timestamp";
$stmt2 = $db->prepare($sql2);
$stmt2->execute();
$res2 = $stmt2->fetchObject();
$best_red = $res2->best_red;
$winning_supplier_red = $res2->company_name;
$win_supplier_red_id = $res2->supplier_id;
$stmt2 = $db->prepare($sql2);
$stmt2->execute();
$res2 = $stmt2->fetchObject();
if (is_object($res2)) {
$best_red = $res2->best_red;
$winning_supplier_red = $res2->company_name;
$win_supplier_red_id = isset($res2->supplier_id) ? $res2->supplier_id : 0;
} else {
$best_red = '';
$winning_supplier_red = '';
$win_supplier_red_id = 0;
}

Mysql query with multiple ands and ors

I'm trying to make a filter function for servers on my site. Users are able to check different options for the categories they want to filter for, and I have an ajax request that returns the servers satisfying their conditions. However, my mysql_query isn't working, I think I might have the wrong syntax.
By default, each category has the option set as 1. My current query is:
$order = "SELECT * FROM `servers`
WHERE `size` = '$size' or
1 = '$size' and
`type` = '$type' or
1 = '$type' and
`mode` = '$gamemode' or
1 = '$gamemode' and
`main` = '$main' or
1 = '$main'
ORDER BY $orderby
LIMIT 5";
It doesn't seem to get the correct servers, do I have an error in my query?
Thanks!
When mixing and and or in your query you must use parenthesis to group the conditions. Also, I think when you say 1 = '$size', I think you mean `size`=1.
$order = "SELECT * FROM `servers`
WHERE
(`size` = '$size' or `size` = '1') and
(`type` = '$type' or `type` = 1) and
(`mode` = '$gamemode' or `mode`= 1 ) and
(`main` = '$main' or `main` = 1)
ORDER BY $orderby
LIMIT 5";
"SELECT * FROM `servers` WHERE
`size` in ('$size', 1) and
`type` in( '$type' ,1) and
`mode` in('$gamemode' ,1) and
`main` in ( '$main' , 1 )
ORDER BY $orderby LIMIT 5";
You need to add parentheses because or and and have a different order of operations and needs parentheses to allow what you are needing to accomplish
SELECT * FROM `servers` WHERE
(`size` = '$size' or 1 = '$size') and
(`type` = '$type' or 1 = '$type') and
(`mode` = '$gamemode' or 1 = '$gamemode') and
(`main` = '$main' or 1 = '$main')
ORDER BY $orderby LIMIT 5
you must have to use brackets to use multiple OR condition in query
like
mysql_query("SELECT * FROM servers WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo')");
you can change with your column name
and also you can use IN condtion like
mysql_query("SELECT * FROM servers WHERE email='$Email' AND date IN ('$Date_Today','$Date_Yesterday','$Date_TwoDaysAgo')");
pls let me know if i can help you more
Just try to put brace around the or conditions
$order = "SELECT * FROM `servers` WHERE (`size` = '$size' or 1 = '$size') and (`type` = '$type' or 1 = '$type') and (`mode` = '$gamemode' or 1 = '$gamemode') and (`main` = '$main' or 1 = '$main') ORDER BY '$orderby' LIMIT 5";

Combining multiple MYSQL queries into one with PHP

I know this subject has been covered before, and I've read about a dozen of the links provided by stackoverflow. None match my need.
I have 4 mysql queries using PHP for similar data, I'd like to lower that to one query and maybe put the results in an array that I can access. Here is my current code.
$id = $row[post_id];
$resulttwo = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'length' ");
$temptwo = mysql_fetch_array($resulttwo);
$length[$id] = $temptwo[0];
$id = $row[post_id];
$resultthree = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_city' ");
$tempthree = mysql_fetch_array($resultthree);
$trailcity[$id] = $tempthree[0];
$id = $row[post_id];
$resultfour = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'location_state' ");
$tempfour = mysql_fetch_array($resultfour);
$trailstate[$id] = $tempfour[0];
$id = $row[post_id];
unset($tempfour);
$resultfour = mysql_query("SELECT meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` = 'difficulty' ");
$tempfour = mysql_fetch_array($resultfour);
$difficulty[$id] = $tempfour[0].' difficulty';`
This should work:
$id = $row[post_id];
$result = mysql_query("SELECT meta_key, meta_value FROM wp_postmeta WHERE `post_id` = $id AND `meta_key` IN ('length', 'location_city', 'location_state', 'difficulty')");
$temp = mysql_fetch_assoc($result);
Array $temp will contain the meta_key along with the meta_value which you should be able to call like so $temp[length]. You can check the entire array with print_r($temp);
You should also stop writing new code using mysql_ functions as they are being deprecated and use mysqli_ or PDO instead.
This should be sufficient as you only care about the first row in each query.
SELECT meta_value FROM wp_postmeta WHERE post_id = $id AND meta_key IN ("length", "location_city", "location_state", "difficulty") LIMIT 4;
Damn too late! :/

pdo binding not giving value

the :cat_id has a value of nine and :tlk_id has a value of 3 if i enter them manually it will run with desired result but after binding them it gives me a row count of zero
<?
$sql = ("
SELECT
p.post_id as sn,
p.subject,
p.username,
COUNT(thread_id)-1 AS responses,
MAX(DATE_FORMAT(p.date_posted,'%e-%b-%y %l: %i %p')) AS first,
MAX(DATE_FORMAT(t.date_posted,'%e-%b-%y %l:%i %p')) AS last
FROM
posts AS p
INNER JOIN
threads AS t
USING
(post_id)
INNER JOIN
register AS u
ON
p.username = u.username
WHERE
t.category_id =:cat_id
AND
p.category_id =:cat_id
AND
t.forum_id = :tlk_id
AND
p.forum_id = :tlk_id
GROUP BY
(t.post_id)
ORDER BY
last DESC
LIMIT $start,$display
");
$sth = $dbh->prepare($sql);
$sth->bindParam(':tlk_id',$tlk_id);
$sth->bindParam(':cat_id',$cat_id);
$r = $sth->execute();
$count = $sth->rowCount();
echo $count;
?>
From the error:
Invalid parameter number: parameter ...
You can see that not all the parameters are processed. So maybe you can add 4 parameters in this case:
$sql = "SELECT
.....
WHERE
t.category_id =:cat_id_1
AND
p.category_id =:cat_id_2
AND
t.forum_id = :tlk_id_1
AND
p.forum_id = :tlk_id_2
....
";
$sth = $dbh->prepare($sql);
$sth->bindParam(':tlk_id_1',$tlk_id);
$sth->bindParam(':tlk_id_2',$tlk_id);
$sth->bindParam(':cat_id_1',$cat_id);
$sth->bindParam(':cat_id_2',$cat_id);
What happens if you do this:
$tlk_id = 3;
$cat_id = 9;
$sth = $dbh->prepare($sql);
$sth->bindParam(':tlk_id',$tlk_id);
$sth->bindParam(':cat_id',$cat_id);
I'm assigning the values to a variable because bindParam expects a reference as a second parameter.

Categories