I have got this php code
$coma_count=substr_count($Hob,',');
if ($coma_count==0) {
$query="SELECT * FROM user_opt WHERE Interests='$Hob' LIMIT 80";
}else{
$expl=explode(',',$Hob);
for ($i=0; $i <=$coma_count; $i++) {
$query.=" UNION SELECT * FROM users WHERE Interests LIKE '%{$expl[$i]}%'";
}
}
$sql=$con->query($query." ORDER BY RAND()") or die($con->error);
Number of $coma_count varies from 0 to 112 and the problem is when $coma_count is 0 (means only country was selected) my query is going to look like this SELECT * FROM user_opt WHERE Country='$Countr' LIMIT 80 ORDER BY RAND() which is not accepted by SQL.What can i do?
Create a $limit variable, set it if need be, then append it:
$limit = '';
$coma_count=substr_count($Hob,',');
if ($coma_count==0) {
$query="SELECT * FROM user_opt WHERE Interests='$Hob'";
$limit = " LIMIT 80";
}else{
$expl=explode(',',$Hob);
for ($i=0; $i <=$coma_count; $i++) {
$query.=" UNION SELECT * FROM users WHERE Interests LIKE '%{$expl[$i]}%'";
}
}
$sql=$con->query($query." ORDER BY RAND()".$limit) or die($con->error);
Related
I have the issue with LIMIT with foreach using PHP.
Basics: I have 50 different tables and in every table I have 2 rows.
When I try to add LIMIT 1 to $$modules_for_all, then I see 50 rows, but I want to see only 1. If I add LIMIT 2, then I see 100 rows.
How I can connect all these tables as a one LIMIT 1 to get 1 row in foreach?
<?php
for ($i = 1; $i <= 50; $i++) {
// $array_table_name contains names with tables
$table_names = $array_table_name[$i];
$modules_for_all = 'g_module_for_all_'.$i;
$$modules_for_all = $db->QueryFetchArrayAll("SELECT * FROM $table_names WHERE user='1' LIMIT 1");
}
for ($i = 1; $i <= 50; $i++) {
$modules_for_from = ${"g_module_for_all_$i"};
foreach ($modules_for_from as $m_foreach_as) {
echo $m_foreach_as['id'];
}
}
Example tables:
table_1
id date_added
1 2018-12-01 00:00:00
2 2018-12-02 00:00:00
table_2
id date_added
1 2018-12-03 00:00:00
2 2018-12-04 00:00:00
table_3
id date_added
1 2018-12-05 00:00:00
2 2018-12-06 00:00:00
Example foreach:
<?php
$array_table_name_1 = 'table_1';
$array_table_name_2 = 'table_2';
$array_table_name_3 = 'table_3';
$for_table_1 = $db->QueryFetchArrayAll("SELECT * FROM $array_table_name_1 WHERE id='1' LIMIT 1 ORDER BY date_added");
$for_table_2 = $db->QueryFetchArrayAll("SELECT * FROM $array_table_name_2 WHERE id='1' LIMIT 1 ORDER BY date_added");
$for_table_3 = $db->QueryFetchArrayAll("SELECT * FROM $array_table_name_3 WHERE id='1' LIMIT 1 ORDER BY date_added");
foreach ($for_table_1 as $m_foreach_as) {
echo $m_foreach_as['id'];
}
foreach ($for_table_2 as $m_foreach_as) {
echo $m_foreach_as['id'];
}
foreach ($for_table_3 as $m_foreach_as) {
echo $m_foreach_as['id'];
}
// Now result is '111' but I want only '1' (realted to make LIMIT 1 to all foreach)
The only way to connect the tables is by using a UNION. So you will need to build one large UNION query and then perform the select after the loop:
$tables = array();
for ($i = 1; $i <= 50; $i++) {
// $array_table_name contains names with tables
$table_names = $array_table_name[$i];
$tables[] = "(SELECT * FROM $table_names WHERE user='1')";
}
$query = implode(" UNION ", $tables) . " ORDER BY date_added LIMIT 1";
$result = $db->QueryFetchArrayAll($query);
foreach ($result as $row) {
echo $row;
}
You are gonna have to use UNION ALL for summing this rows together before ordering and limiting the results.
But keep in mind that a query like this will only work if all the tables in your array have the same structure. If they do not, then you will have to be specific in the query to make them have the same fields.
$array_table_name = [
'table_1',
'table_2',
'table_3',
];
$search_id = 1;
$selectsArray = [];
foreach ($array_table_name as $table_name) {
$selectsArray[] = "SELECT * FROM $table_name WHERE id='$search_id'\n";
}
As you see, I am using foreach() and not for() so you won't update the for by decreasing/increasing the number of tables in the array. So to finally have:
$selectsUnion = implode("UNION ALL\n", $selectsArray) . "ORDER BY date_added \nLIMIT 1";
You can see the code tested and query mounted here: https://3v4l.org/HXH2K
I solved my problem using this: foreach ($modules_for_from as $m_foreach_as) if ($tmp++ < 1) {
On the memberpage its shows the first 16 members, they are all in alphabetical order.
How to change that into random.
<?php
$limit=48;
$stages ='none';
$page = isset($_GET['page'])?mysql_escape_string($_GET['page']):0;
$start = ($page !== 0)?($page - 1) * $limit:0;
$queryl ="select *, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthdate)), '%Y')+0 as age from user_profiles LIMIT ".$start.",".$limit;
$query = "SELECT count(*) as num FROM `user_profiles` ";
$targetpage = 'zoeken.php?'.rtrim($_SERVER['QUERY_STRING'],'&');
$dis = display($queryl);
$paginate = paginate2($page, $stages, $limit, $targetpage, $query, $start);//current page, stages, limit, query
echo $dis['thumbnail'];
?>
Select a random row with MySQL:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
select *, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthdate)), '%Y')+0 as age from user_profiles ORDER BY RAND() LIMIT ".$start.",".$limit;
is there a better way to return random entries from the TOP 200 bestselling tshirts (tshirt_sales from shop_tshirts) that could just query just 6 entries instead of 200 ?
$SQL = "SELECT * FROM shop_tshirts WHERE shop = 'nidieunimaitre' AND online='1' ORDER BY tshirt_sales DESC LIMIT 200";
$Result = mysql_query($SQL)
or die('A error occured: ' . mysql_error());
$Rows = array();
while ($Row = mysql_fetch_assoc($Result))
$Rows[] = $Row;
shuffle($Rows);
$i = 0;
foreach($Rows as $Data){
$i++;
if($i >= 6) { break; }
}
Try this:
SELECT *
FROM
(SELECT *
FROM shop_tshirts
WHERE shop = 'nidieunimaitre' AND online='1'
ORDER BY tshirt_sales DESC
LIMIT 200) as tableAlis
ORDER BY RAND()
LIMIT 6;
Return first 200 results and after order by rand and return only six results.
I need to combine the two queries if possible or make them process one after the other. I'm assuming the $Record_Count = $Record_Count + 1; doesn't need to be there twice since that's just for the pagination script. (thanks in advance)
$results = mysql_query("SELECT * load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
while ($data = mysql_fetch_array($results)) {
$Record_Count = $Record_Count + 1;
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
while ($recdata = mysql_fetch_array($rec_res)) {
$Record_Count = $Record_Count + 1;
IF $Record_Count is just counting the number of returned rows you could always use mysql_num_rows()
$results = mysql_query("SELECT * FROM load_test WHERE language = '".$lang."' ORDER BY Id DESC, creationdate DESC LIMIT $start, 5");
$rec_res = mysql_query("SELECT * FROM names WHERE com_id = '".$data[Id]."'");
$Record_Count += mysql_num_rows($result) + mysql_num_rows($rec_res);
how do I order this result??
$range = 5; // you'll be selecting around this range.
$min = $rank - $range;
$max = $rank + $range;
$limit = 10; // max number of results you want.
$result = mysql_query("select * from table where rank between $min and $max limit $limit");
while($row = mysql_fetch_array($result))
{
echo $row['name']." - ".$row['rank']."<br>";
}
$result = mysql_query(
"select * from table where rank between $min and $max " .
"order by rank asc limit $limit"
);
Use the "order by"- clause:
mysql_query("select * from table where rank between $min and $max order by rank limit $limit");
This will order your result from little to big values.
Use "order by rank desc" to order in descendent direction. (big -> little)