Where to put the DESC LIMIT [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Where do i put the Desc limit, doesnt work to put it at the end :/
$query = "SELECT * FROM category INNER JOIN videos ON videos.catid=category.id WHERE videos.favorite = '1' DESC LIMIT 5";
I only get warning
: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in

Try this:
SELECT * FROM category
INNER JOIN videos ON videos.catid=category.id
WHERE videos.favorite = '1'
ORDER BY category.id DESC LIMIT 5
I have used ORDER BY category.id, but you may use your preferable field.

Related

SQL: ORDER BY, AND, WHERE returns boolean error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Trying just to order some ids by the date if their status is not active (aktiv = nei) and if its less then 7 days ago they where created, but i'm getting the "mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given" error, and I can't seem to figure out why.
I'm following the general order of caluses (SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY). It works if i remove ORDRE BY or the date selection.
$aktivnei = 'SELECT * FROM `test3` WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE(); ORDER BY datex DESC';
$resultaktivnei = mysqli_query($conn,$aktivnei);
<?php
while ($row = mysqli_fetch_assoc($resultaktivnei)) {
echo '<li class="list-group-item">'.$row['id'].' / '.strftime('%H:%M, %e.%b',strtotime($row['datex'])).'</li>';
};
?>
I have also tried this, but getting the exact same error. I have also tried moving the caluses around but to no success.
$aktivnei = 'SELECT * FROM `test3`';
$aktivnei .= ' WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE();';
$aktivnei .= ' ORDER BY datex DESC';
It's probably dead simple, but right now i'm just scratching my head after trying so many different things.
remove semicolon before order by
'SELECT * FROM `test3` WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE() ORDER BY datex DESC'

Using php variables in ORDER BY statement gives error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
The following is my query:
$sql = "SELECT date, status, reason
FROM tbl_attendance_mgmt ORDER BY date $order
LIMIT $number
WHERE fk_stu_id = '$stu_id'";
Here:
$order: ASC or DESC
$number: Number of rows to be displayed.
Can someone please help me out why this query gives me the error while executing it?
You must have follow sequence for sql query. WHERE place before order by clause
$sql = "select `date`, status, reason from tbl_attendance_mgmt where fk_stu_id = '$stu_id' order by `date` $order limit $number";
Write your query this way:
$sql = "SELECT date, status, reason
FROM tbl_attendance_mgmt
WHERE fk_stu_id = '{$stu_id}'
ORDER BY date $order
LIMIT $number ";
You have to bind your php variables using curly braces{}
$sql = "SELECT date, status, reason
FROM tbl_attendance_mgmt
WHERE fk_stu_id = " . mysqli_real_escape_string($con, $stu_id) . "
ORDER BY date $order
LIMIT $number ";
You should escape your variables
A query should be in this order: SELECT, FROM, WHERE, ORDER BY, and LIMIT:

Count Days Student has Attended [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to count up the days a student has attended class.
I need it to display the total attendance based on what member_id is logged in.
This is what I have so far:
$id_member = 3;
$query = "SELECT COUNT(attendance) AS day_at FROM day_attendance WHERE id_member=$id_member";
$response = #mysqli_query($dbc, $query);
$row = mysqli_fetch_array($response);
$day_at = $row["day_at"];
echo $day_at;
It throws this error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /usr/www/user1/web/data/test.php on line 737
I think you misspelled the id column, should be member_id (as in your screen) and not id_member as in your sql query.
That's why mysqli_fetch_array print you a warning, because mysqli_query fail, and when it fail it returns a boolean (false). (Btw it's really bad to use the # character)

PHP T string error for sql statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm having a strange error with my PHP code. it says there is a unexpected T_CONSTANT_ENCAPSED_STRING error, but I simply can't see it.
$dateResult = mysqli_query($connection, 'select county, cuisine,
count(*) from inspection, restaurant where inspection.rid =
restaurant.rid and inspection.passfail = ''PASS'' and idate like '$date%'
group by county, cuisine');
I'm assuming its an issue with either my like pattern '$date%' or with 'PASS'.
Thank you!
Try this your are conflicting ' and "
$dateResult = mysqli_query($connection, "select county, cuisine,
count(*) from inspection, restaurant where inspection.rid =
restaurant.rid and inspection.passfail = 'PASS' and idate like '$date%'
group by county, cuisine");

multiple number select statement sql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
looking for something like
Select * FROM 'database' WHERE id = 1-40
something where I can select a number through a number like 1 through 40
multiple number select statement sql
SELECT * FROM database_table WHERE id BETWEEN 1 AND 40
This is an example page you can use: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_between
The between operator is what you're looking for:
SELECT * FROM `database` WHERE id BETWEEN 1 AND 40
Try:
Select * FROM `tablename` WHERE id >= 1 AND id <= 40
Two possible ways for selecting range in DB:
Select * FROM `database.tablename` WHERE id BETWEEN 1 AND 40 ;
or
SELECT * FROM `DATABASE.TABLENAME` WHERE ID >= 1 AND <=40;
In case, you want to select multiple items which are not in range, you can use:
Select * FROM `database.tablename` WHERE id in (1,2,3,4,5,6.....40);

Categories