Error when using "ORDER BY count" in query - php

I'm using the following query to display some information:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
My issue is it works fine when I leave out ORDER BY count DESC but when it is there I get the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /proj/co600/project/repo/public_html/select_field3.php on line 227
Count is a column in my database which records the number of times a publication is downloaded.

count is an aggregate function, so you need to surround it with backticks.
To get a clear cut picture of your error.. You need to change your code like..
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
if(!$result)
{
die(mysqli_error($con));
}

You are having MySQL reserved keyword as column name in table.
Use Below query:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY `count` DESC ");

Related

PHP, SQL limit query by php variable

PHP code defining variable sqlshowvalue
$sqlshowvalue = 5;
if(isset($_POST['showmore'])) {
$sqlshowvalue += 5;
}
So I connect to my database and then when I run this SQL query below using the variable that I just defined above,
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");
So I am using mysqli as the method to connect to my DB and it gives me the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in ..
The reason it gives me this error is because something in my query is wrong and what it has to do is $sqlshowvalue, because if I replace sqlshowvalue with with just the number 5 (like shown below), it works fine:
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit 5");
So I am just wondering what I can do to make it so that the value for the limit is a PHP variable that I can be changed and the page updated.
Have you tried making
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");
to
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit ".$sqlshowvalue);
Remove '' use only $sqlshowvalue:-
$result = mysqli_query($conn,"SELECT * FROM comments
ORDER BY id DESC limit $sqlshowvalue");
for integer value no need of ''

Creating a subquery with mysqli in PHP to fetch array last 10 results in ascending order

I thought this would be simple but I'm having a tough time figuring out why this won't populate the the data array.
This simple query works fine:
$queryPrice = "SELECT price FROM price_chart ORDER BY id ASC LIMIT 50";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
But instead I want it to choose the last 10 results in Ascending order. I found on other SO questions to use a subquery but every example I try gives no output and no error ??
Tried the below, DOESN'T WORK:
$queryPrice = "SELECT * FROM (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
I also tried specifying the table name again and using the IN, also doesn't work:
$queryPrice = "SELECT price FROM price_chart IN (SELECT price FROM price_chart ORDER BY id DESC LIMIT 10) ORDER BY id";
$resultPrice = mysqli_query($conn, $queryPrice);
$data = array();
while ($row = mysqli_fetch_array($resultPrice)) {
$data[] = $row[0];
}
In both examples my array is blank instead of returning the last 10 results and there are no errors, so I must be doing the subquery wrong and it is returning 0 rows.
The subquery doesn't select the id column, so you can't order by it in the outer query. Also, MySQL requires that you assign an alias when you use a subquery in a FROM or JOIN clause.
$queryPrice = "SELECT *
FROM (SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) x ORDER BY id ASC";
$resultPrice = mysqli_query($conn, $queryPrice) or die (mysqli_error($conn));
$data = array();
while ($row = mysqli_fetch_assoc($resultPrice)) {
$data[] = $row['price'];
}
You would have been notified of these errors if you called mysqli_error() when the query fails.
Your second query is the closest. However you need a table alias. (You would have seen this if you were kicking out errors in your sql. Note you will need to add any field that you wish to order by in your subquery. In this case it is id.
Try this:
SELECT * FROM (SELECT price, id
FROM price_chart ORDER BY id DESC LIMIT 10) as prices
ORDER BY id ASC
You must have errors, because your SQL queries are in fact incorrect.
First, how to tell you have errors:
$resultPrice = mysqli_query (whatever);
if ( !$resultprice ) echo mysqli_error($conn);
Second: subqueries in MySQL need aliases. So you need this:
SELECT * FROM (
SELECT id, price
FROM price_chart
ORDER BY id DESC LIMIT 10
) AS a
ORDER BY id ASC";
See the ) AS a? That's the table alias.

Query 2 tables join

I want to get all the films whose genre is equal to ID_GENRE = 8. This is the query that I'm doing:
http://l4c.me/fullsize/2-tablas-1434140362.png
$query_GetSimilar = sprintf("SELECT * FROM z_movie,z_movie_genre ORDER BY z_movie.visits DESC WHERE z_movie_genre.id_genre = 8 LIMIT 18");
$GetSimilar = mysql_query($query_GetSimilar, conect::dbconect()) or die(mysql_error());
$row_GetSimilar = mysql_fetch_assoc($GetSimilar);
$totalRows_GetSimilar = mysql_num_rows($GetSimilar);
But I jump the next error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE z_movie_genre.id_genre = 8 LIMIT 18' at line 1
Note:
WHERE should be first before ORDER BY
You can use INNER JOIN to get two connecting tables, assuming that they have connecting id/column.
For example, both z_movie and z_movie_genre table has the column id_genre to connect each other, you can try this:
$query_GetSimilar = sprintf("SELECT * FROM z_movie
INNER JOIN z_movie_genre ON z_movie.id_genre = z_movie_genre.id_genre
WHERE z_movie_genre.id_genre = 8
ORDER BY z_movie.visits DESC
LIMIT 18");
Make a simple search, and see if you can put 'order by' before 'where'.
ok, now my query is:
$query_GetSimilar = sprintf("SELECT * FROM z_movie,z_movie_genre WHERE z_movie_genre.id_genre=8 ORDER BY z_movie.visits DESC LIMIT 18");
$GetSimilar = mysql_query($query_GetSimilar, conect::dbconect()) or die(mysql_error());
$row_GetSimilar = mysql_fetch_assoc($GetSimilar);
$totalRows_GetSimilar = mysql_num_rows($GetSimilar);
but I do not have the expected results

SQL query doesn't give any results in PHP

I am using the following query:
$query = "SELECT (SELECT count(*) FROM survey_ptw WHERE erfh= '0') AS F01,
(SELECT count(*) FROM survey_ptw WHERE erfh='10') AS F02,
(SELECT count(*) FROM survey_ptw WHERE erfh='50') AS F03";
$result = mysql_query($query);
print sprintf('Erf: <table><tr><td>none</td><td>%s</td></tr><tr><td>more</td><td>%s</td></tr></table>', $F01,$F02);
When I'm doing the above query in phpMyAdmin, it displays the variables nicely with the correct result values. Doing the same in PHP however, no results are displayed (empty strings)!
mysql_errno() and mysql_error() don't return errors. The DB has been open and selected further up in the code.
Any idea what's going on?
Because you aren't doing anything with the results.
From the documentation for mysql_query():
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
So, to retrieve the results, you can use a while loop as below:
while ($row = mysql_fetch_assoc($result)) {
# code...
}
check $res=mysql_fetch_assoc($result); then print_r(); to check the result. it will work.
$query = "SELECT (SELECT count(*) FROM survey_ptw WHERE erfh= '0') AS F01,
(SELECT count(*) FROM survey_ptw WHERE erfh='10') AS F02,
(SELECT count(*) FROM survey_ptw WHERE erfh='50') AS F03";
$result = mysql_query($query);
$res=mysql_fetch_assoc($result);
$res=$res[0];
sprintf('Erf: <table><tr><td>none</td><td>%s</td></tr><tr><td>more</td><td>%s</td></tr></table>', $res['F01'],$res['F02']);

SQL_CALC_FOUND_ROWS not working

so I execute query:
SELECT SQL_CALC_FOUND_ROWS * FROM table l LIMIT 10, 20;
which returns 20 rows from table, and there are a total 553 rows in the table
Then i immediately execute SELECT FOUND_ROWS();
But this instead only returns the number 1, despite the fact that there are 553 rows in my table (it's supposed to return 553, am I correct?)
what did I do wrong?
I suspect you have a syntax error, as names in SQL are not supposed to contain spaces. Try adding square brackets around [table 1], if that is the name of your table.
Remove your limit, I think there it is not showing 20 rows; it is showing only 10 rows.
$sql = "SELECT SQL_CAL_FOUND_ROWS * FROM users "; //don;t write table then table name
$result = mysql_query($sql);
//Use these according to your requirements:
$sql = "SELECT SQL_CAL_FOUND_ROWS * FROM users ";
$result = mysql_query($sql);
$sql = "SELECT FOUND_ROWS() AS `found_rows`";
$rows = mysql_query($sql);
$rows = mysql_fetch_assoc($rows);
$total_rows = $rows['found_rows'];

Categories