sql query is not working after adding descending order conditions - mysql

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"

Related

Can I check which condition after where was true and got executed in my query?

I have a get parameter and I want to select from my table based on this get parameter, But I have to check the two conditions after where if the first was not equal then move to the next one. But the main problem is I want to know which condition was executed the first one or the second one.
$sql = $db_->prepare("Select * from table_x where url_en = ? Or url_fr = ?");
Instead of doing this:
$sql = $DB_->prepare("select * from tbl_items where `url_ar`= ?");
$sql->bind_param("s", $_GET["url"]);
$sql->execute();
$result = $sql->get_result();
if($result->num_rows == true){$lang = "ar";}else{
$sql = $DB_->prepare("select * from tbl_items where `url_fr`= ?");
$sql->bind_param("s", $_GET["url"]);
$sql->execute();
$result = $sql->get_result();
if($result->num_rows == true){$lang = "fr";}else{
echo "unfound";exit;
}
}
I want something short by using just SQL
Try searching up each value independently or try bringing them up together in a list and see where they reside when trying to organize it with ORDER BY.
You can use UNION and execute each condition separately. Using some kind of label you can then find out which condition was true.
SELECT 'EN' as label, * FROM table_x WHERE url_en = ?
UNION
SELECT 'FR' as label, * FROM table_x WHERE url_fr = ?

I have MySQL problem: ORDER BY DESC POSTS

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"

as I order the result to alphabetical order?

as I order the result to alphabetical order?
need it to read the directory and add the files in alphabetical order in mysql ...
$path = "artes110/$grupo";
$diretorio = opendir($path);
while ($arquivo = readdir($diretorio)){
if ($file != '.' && $file != '..')
{
$arquivo = utf8_encode($arquivo);
$arquivo = html_entity_decode($arquivo);
$query = mysql_query("INSERT INTO artes101 (imagem, data, texto, layout, grupo) VALUES ('$path/$arquivo', '$data2', '$texto', 'peq', '$grupo')") or die(mysql_error());
}
}
In mysql you can add them in any order and then get them ordered by adding an order by clause in your SQL code.
SELECT * FROM artes101 ORDER BY imagem DESC;
You can change imagen to whatever field you want them sorted by, and can change the direction of the order by changing the DESC to ASC

Mysql: Possible errors in queries "WHERE...IN" , "ORDER BY..." and "LIMIT"

As you have observed I have put an ambiguous title for this question, as simply I do not realize (by lack of deep knowledge) if this is the true problem or not.
Let's start then with a short description:
Here is where I select my products:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array)";
After that here is where I define the pagination stuff:
$query_page = mysql_query($select_prod);
$product_total = mysql_num_rows($query_page);
$page_rows = 4;
$last = ceil($product_total/$page_rows);
if ($page < 1) {
$page = 1;
} elseif ($page > $last) {
$page = $last;
}
$limit = 'limit ' .($page - 1) * $page_rows .',' .$page_rows;
And where I prepare the render
$page_query = mysql_query($select_prod . $limit);
$results = array();
while ($array_filter = mysql_fetch_array($page_query)) {
$results[] = $array_filter;
}
Until this point everything is flowing easily, and I get my products listed as I wanted, BUT in random ORDER.
I have tried to include "ORDER BY price ASC" at the end of the first query like this:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array) ORDER BY price ASC";
but for a strange reason fails to list the products with the error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given...
I've already had several hours in trying to find where could be the problem, and this forum seems to be the final try for me, after that I would let them order as they want to.
You really need to print out your full query directly before execution. Try this instead:
$select_prod = "SELECT * FROM product WHERE product_id IN ($some_array) ORDER BY price ASC ";
Or, change the limit line to have a space before the limit.
What I believe the problem is the the lack of space before limit. The snippet in ()limit 100 is valid SQL. The snippet in () order by price asclimit 100 is not valid SQL.

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'
...