How to limit time in mysql for search function - mysql

I have one problem.
I make search function in php. I need limit searching only for 1 mounth ago.
ex.
I put 'android' word
In result show only titles who containing android openet in last mounth. Oldest no show.
Thank you
EDIT
$sql = '
SELECT aa_search_index.content_id, MATCH(aa_search_index.title, aa_search_index.metadata) AGAINST (?) AS score
FROM aa_search_index
LEFT JOIN aa_thread
ON (aa_thread.thread_id = aa_search_index.content_id)
WHERE MATCH(aa_search_index.title, aa_search_index.metadata) AGAINST (?)
AND content_type = ?
AND content_id != ?
';
$params = array($query, $query, 'thread', $excludedThreadId);
if (!Search_query::get('options')->showResultsGlobal)
{
$params[] = $forum['node_id'];
$sql .= 'AND aa_thread.node_id = ?
';
}
$sql .= 'LIMIT 5';
$results = $this->fetchAllKeyed($sql, 'content_id', $params);
$threadIds = array_keys($results);

select * from your_table
where column_with_text = 'android'
and datetime_column >= curdate() - interval 1 month

Related

Get maximum value of row in mysql ,using codeigniter

Get maximum value of row in mysql using codeigniter.
i have following mysql data
I need to get the details based on total_payment of each student.
like this
i tried code below
$student_id_dis = $this->db->query('SELECT DISTINCT(student_id) FROM student_fees')->result_array();
$fee_cat_id_dis = $this->db->query('SELECT DISTINCT(fee_category_id) FROM student_fees')->result_array();
$this->db->select(['student_fees.*', 'fee_categories.fee_category_name as fee_name', 'fee_categories.amount as fee_amount']);
$this->db->join('student', 'student_fees.student_id = student.student_id');
$this->db->join('fee_categories', 'student_fees.fee_category_id = fee_categories.fee_categories_id');
$where = '';
for ($i = 0; $i < count($student_id_dis); $i++) {
if (isset($fee_cat_id_dis[$i]['fee_category_id'])) {
$where .='total_paid = (SELECT max(stdp.total_paid)
FROM student_fees stdp
WHERE stdp.fee_category_id = ' . $fee_cat_id_dis[$i]['fee_category_id'] . ')';
}
$this->db->where($where);
$this->db->get('student_fees')->result_array();
}
try this
select * from student_fees where student_fees_id in
(select student_fees_id from
where total_paid =max(total_paid) group by fee_category_id)

Trying to sum MySQL value

I'm trying to sum my MySQL column value with number, here is my php script, i dont think im doing in right way. I was searching in stackoweflow but i havent found how to sum with specific number, not with other column.
<?php
$currentUser = isset($_POST['currentUser']) ? $_POST['currentUser'] : '';
$currentTasken = isset($_POST['currentTasken']) ? $_POST['currentTasken'] : '';
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());
$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows >= 1)
{
//$pass = md5($pass);
$ins = mysql_query("INSERT INTO dotp_task_log (task_log_creator, task_log_Task) VALUES ('$currentUser' , '$currentTasken')" ) ;
if($ins)
$check = mysql_query("SELECT * FROM dotp_task WHERE task_id='$currentTasken'");
$numrows = mysql_num_rows($check);
if($numrows == 1)
{
//$pass = md5($pass);
$ins = mysql_query("SELECT (SELECT SUM(task_percent_complete) FROM dotp_task WHERE task_id='$currentTasken') FirstSum, (SELECT SUM(5)), SecondSum ");
if($ins)
die("Succesfully summed!");
else
die("ERROR");
}
else
{
die("Cant sum!");
}
die("Succesfully Created Log!");
else
die("ERROR");
}
else
{
die("Log already exists!");
}
?>
Your query is not written properly. Try to run this query from MySQL command line or from PHPMyAdmin. It will give you an idea what the error is. My best guess is that the first part of the query is not complete.
This is what it will resolve to
SELECT n FirstSum, 5, SecondSum
Where n is the value returned from the subquery. This is a syntax error. Try to remove the last comma before SecondSum.
This is what your query should be:
SELECT (SELECT Sum(task_percent_complete)
FROM dotp_task
WHERE task_id = '$currentTasken') FirstSum,
(SELECT Sum(5)) SecondSum
remove
(select sum(5))
and replace it with
(select 5)

Dynamic mysql query LIMIT not working

I'm creating the parameters to LIMIT a mysql query using PHP variables. I'm outputting the query to make sure it's right, and it is. However the results completely ignore the offset parameter. However if I manually type in the LIMIT, it works fine.
$page_rows = 5;
$max = ($pagenum - 1) * $page_rows .',' .$page_rows;
$qry = "SELECT * FROM ".$wpdb->prefix."nslikethis_votes WHERE user_id = '$uid' AND active = 1 ORDER BY time DESC LIMIT " . $max;
$rs = mysql_query($qry);
$result = array();
if($rs && $rows>0){
while($lc = mysql_fetch_object($rs, "LikedContent")){
$result[] = $lc;
}
}
return $result;
Outputting $qry gives me this whether I use $max or manually enter '5,5':
SELECT * FROM wp_nslikethis_votes WHERE user_id = '1' AND active = 1 ORDER BY time DESC LIMIT 5,5
Try doing this for check:
$page_rows = "5";
$max = ($pagenum - 1) * $page_rows .',' .$page_rows;
$qry = "SELECT * FROM ".$wpdb->prefix."nslikethis_votes WHERE user_id = '$uid' AND `active` = 1 ORDER BY `time` DESC LIMIT " . $max;
//check for variables in query like $wpdb->prefix, $uid: are they fine, and try adding tilded sign as above
Hope this works for you

Get only today posts?

i need to have a function that will echo the post that has been posted today.
from 00.00 to 23.59 . ihave created a function but it is not giving the result i need
here is my function:
function todayNews() {
$id = mysql_real_escape_string ($id);
$sql = 'SELECT * FROM wp_posts WHERE post_status = "publish" AND post_date = CURRENT_DATE ORDER BY post_date DESC LIMIT 15';
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0):
while ($row = mysql_fetch_assoc($res)) {
$title = ($row['post_title']);
$old_date = $row['post_date']; // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('H:i', $old_date_timestamp);
$mycontent = ($row['post_content']);
$mycontent = strip_tags($mycontent);
$mycontent = substr($mycontent,0,350);
$mycontent = preg_replace("/\[caption.*\[\/caption\]/", '', $mycontent);
$first_img = '';
$my1content = $row['post_content'];
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/img/default.png";
}
echo '
<ul class="li-sub">
<li>
'.$title.'
</li>
</ul>
';
}
else:
echo 'There are no posts for today !';
endif;
} // end
Thank you !
EDIT:
Post_date have this format : Y-m-d H:i
You are comparing POST_DATE which is a datetime with CURRENT_DATE which is a Date , it won't give any result.
you can try the following SQL
$sql = 'SELECT * FROM wp_posts WHERE post_status = "publish" AND DATE(post_date) = CURRENT_DATE ORDER BY post_date DESC LIMIT 15';
This will convert the post_date to only date before comparing with current date.

mySQL query from two different tables with mutual conditional

Please help me to structure mysql query
I have 2 tables, #_udjacomment AND #_content
currently I have query:
$query = "SELECT udja.id";
if( $include_author == 1 ) $query .= ", udja.full_name";
if( $include_date == 1 ) $query .= ", udja.time_added";
if( $include_comment == 1 ) $query .= ", if(CHAR_LENGTH(udja.content) > ".$content_number_of_characters.", SUBSTR(udja.content, 1, ".$content_number_of_characters."), udja.content) AS content";
if( $include_link_to_comment == 1 ){
$query .= ", CASE WHEN LOCATE('com_content:', udja.comment_url) > 0
THEN CONCAT(SUBSTRING_INDEX(udja.comment_url,':',-1),'-', com_content.alias, '.html')
ELSE udja.comment_url END AS comment_url";
}
$query .= " FROM #__udjacomments AS udja, #__content AS com_content WHERE udja.is_published = 1 AND com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1) AND com_content.checked_out = 0 ORDER by udja.id DESC limit ".$number_of_comments;
But I am not getting the proper results. If I stop trying to access from the table #__content AS com_content, then I get the results for #__udjacomment AS udja correct
So, I guess I am asking how can indicate and include the constrain that I want the field com_content.alias WHERE com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1)
In some cases, udja.comment_url will have this format com_content:22, com_content:19
and in other instances, udja.comment_url will have a string like word-word-another-word
this is why I have the more extensive statement inside the conditional if($include_link_to_comment == 1)
UPDATE: THE FINAL QUERY LOOKED LIKE THIS (I IMPLEMENTED WHAT RESPONDER SUGGESTED AND CHANGED THE CASE STATEMENT AND THE WHERE STATEMENT)
$query = "SELECT udja.id";
if( $include_author == 1 ) $query .= ", udja.full_name";
if( $include_date == 1 ) $query .= ", udja.time_added";
if( $include_comment == 1 ) $query .= ", if(CHAR_LENGTH(udja.content) > ".$content_number_of_characters.", SUBSTR(udja.content, 1, ".$content_number_of_characters."), udja.content) AS content";
if( $include_link_to_comment == 1 ){
$query .= ", CASE
WHEN LOCATE('com_content:', udja.comment_url)<>0
THEN CONCAT(SUBSTRING_INDEX(udja.comment_url,':',-1),'-', com_content.alias, '.html')
ELSE udja.comment_url
END AS comment_url";
}
// THEN CONCAT(SUBSTRING_INDEX(udja.comment_url,':',-1),'-', com_content.alias, '.html')
$query .= " FROM #__udjacomments AS udja
LEFT JOIN #__content AS com_content
ON com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1)
WHERE udja.is_published = 1 ORDER by udja.id DESC limit ".$number_of_comments;
You need to use an outer join:
...
FROM #__udjacomments AS udja
LEFT JOIN #__content AS com_content
on com_content.id = SUBSTRING_INDEX(udja.comment_url,':',-1)
WHERE ...