I have MySQL problem: ORDER BY DESC POSTS - mysql

I have one problem. I want to order by desc for posts.
case 'posts':
// page header
page_header($system['system_title'].' - '.__("Posts Directory"));
// pager config
require('includes/class-pager.php');
$params['selected_page'] = ( (int) $_GET['page'] == 0) ? 1 : $_GET['page'];
$total = $db->query("SELECT * FROM posts") or _error(SQL_ERROR);
$params['total_items'] = $total->num_rows;
$params['items_per_page'] = $system['max_results'];
$params['url'] = $system['system_url'].'/directory/'.'posts'.'/%s';
$pager = new Pager($params);
$limit_query = $pager->getLimitSql();
// get posts
$rows = array();
$get_rows = $db->query("SELECT post_id FROM posts ".$limit_query) or _error(SQL_ERROR);
while($row = $get_rows->fetch_assoc()) {
$row = $user->get_post($row['post_id']);
if($row) {
$rows[] = $row;
}
}
$smarty->assign('rows', $rows);
$smarty->assign('pager', $pager->getPager());
break;
I can try order by post_id desc? Please help my questions.

In your query statement :
$get_rows = $db->query("SELECT post_id FROM posts ".$limit_query) or _error(SQL_ERROR);
You can change to
$get_rows = $db->query("SELECT post_id FROM posts Order By post_id DESC".$limit_query) or _error(SQL_ERROR);
I am do change in query statement get row post.. Because i don't know the use for total query statement and the select statement same too. So your question about how to do Order By Desc for Posts that's what i recommended..

Simply add this "ORDER by {field_name} DESC" to your SQL query, for example:
"SELECT * FROM posts ORDER by post_id DESC"

Related

sql query is not working after adding descending order conditions

Well, I have a list of jobs that I want to display so candidates can apply for the job. The problems is, the listing keep showing the old jobs and not the new one. I try to put the DESC query but it's become error.
This is what I have on my database;
and when I input the original query, it's working
original;
<?php
$limit = 4;
$sql = "SELECT COUNT(id_jobpost) AS id FROM job_post";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$total_records = $row['id'];
$total_pages = ceil($total_records / $limit);
} else {
$total_pages = 1;
}
?>
and this one is after I add the DESC
$sql = "SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY DESC";
but this error came out
Trying to get property of non-object in C:\xampp\htdocs\jobportal\jobs.php on line 148
and the result doesn't come out in descending order.
what should I do to resolve this?
Thank you in advance.
The Syntax is:
SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY column1 DESC;
Here column1 refers to the column that is to be sorted.
You need column name in your order by clause.
SELECT COUNT(id_jobpost) AS idCount FROM job_post ORDER BY idCount DESC
you have missed the column you want to order your result by:
"SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY id DESC"

How to limit time in mysql for search function

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

Writing a custom mysql query to order wordpress users by a custom user meta field

Basically, I need to order a list of WordPress users by the city they live in. I've got a custom user meta field for city, and I've got the query working properly, but the query lists everyone who hasn't filled out a city at the beginning since it places blank fields at the beginning of the order.
What I need is to figure out how to only select and display users who have given a value other than blank in the city field. Unfortunately, I've found myself stumped.
Any thoughts on how to do this? Also, if anyone knows a way to orderby a custom user meta field using wp_user_query as opposed to this mess, I'm all ears.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$limit = 10;
$offset = ($paged - 1) * $limit;
$key = 'city';
$sql = "SELECT SQL_CALC_FOUND_ROWS {$wpdb->users}.* FROM {$wpdb->users}
INNER JOIN {$wpdb->usermeta} wp_usermeta ON ({$wpdb->users}.ID = wp_usermeta.user_id)
INNER JOIN {$wpdb->usermeta} wp_usermeta2 ON ({$wpdb->users}.ID = wp_usermeta2.user_id)
WHERE 1=1
AND wp_usermeta.meta_key = 'wp_capabilities'
AND CAST(wp_usermeta.meta_value AS CHAR) LIKE '%\"subscriber\"%'
AND wp_usermeta2.meta_key = '$key'
ORDER BY wp_usermeta2.meta_value ASC
LIMIT $offset, $limit";
$site_users = $wpdb->get_results($sql);
$found_rows = $wpdb->get_var("SELECT FOUND_ROWS();");
foreach ($site_users as $site_user) {
// user info here
}
Try something like
...
WHERE 1=1
AND wp_whatever.name_of_city IS NOT NULL
AND LENGTH(wp_whatever.name_of_city) > 0
AND wp_usermeta.meta_key = 'wp_capabilities'
...

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

Wordpress list results from wp_users and wp_usermeta

I am writing a plugin which grabs an array of data from the wordpress database using...
$data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);
This works fine and I can display all the info from the users table, the issue I have is that I need to also pull out the First name and Last name which are in the wp_usermeta table.
Is there a way to modify the statement to also pull those details from the other table?
Not in one query and not necessarily the slickest either, but the following will produce what you want:
$data = $wpdb->get_results("SELECT * FROM $wpdb->users", ARRAY_A);
$i = 0;
foreach($data as $single) {
$meta = $wpdb->get_results(
"SELECT meta_value
FROM $wpdb->usermeta
WHERE user_id = $single[ID]
AND (meta_key = 'first_name' OR meta_key = 'last_name')
ORDER BY meta_key",
ARRAY_A
);
$data[$i]['first_name'] = $meta[0]['meta_value'];
$data[$i]['last_name'] = $meta[1]['meta_value'];
$i++;
}
EDIT: Here it is in one query:
$data = $wpdb->get_results(
"SELECT $wpdb->users.*,
GROUP_CONCAT(
$wpdb->usermeta.meta_value
ORDER BY $wpdb->usermeta.meta_key
SEPARATOR ' '
) AS name
FROM $wpdb->users
LEFT JOIN $wpdb->usermeta
ON $wpdb->users.ID = $wpdb->usermeta.user_id
WHERE ($wpdb->usermeta.meta_key = 'first_name'
OR $wpdb->usermeta.meta_key = 'last_name')
GROUP BY $wpdb->users.ID",
ARRAY_A
);
Note that opposed to the first version, the latter does not produce $data[x]['first_name'] and $data[x]['last_name'], but $data[x]['name'] instead. This is due to either being stored in the "meta_value" column. It is not possible to accomplish your task in one query and store the first and last name in two different array keys at the same time.
Hence, if doing it the second way, you'd have to use php's explode() function later on to access the name. Or correct it in a loop after the query has been run:
$i = 0;
foreach($data as $single) {
$name_parts = explode(' ', $single['name']);
$data[$i]['first_name'] = $name_parts[0];
$data[$i]['last_name'] = $name_parts[1];
$i++;
}