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:
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
I am trying to use code from the following old question:
MySQL: Count occurrences of distinct values
My query is as follows:
$result = $db->query("SELECT name,COUNT(*) as cnt FROM `table` GROUP BY name ORDER BY cnt DESC");
$row = mysqli_fetch_array($result);
var_dump($row);
In phpmyadmin this code will output all the name and COUNT columns. When I run var_dump($row) it will only have one row for me to work with (the first one, ie the one with the most occurrences), which I can't figure out why. Any ideas? Thanks.
From the documentation on mysqli_fetch_array:
Fetch a result row as an associative, a numeric array, or both
As you found out, it does just that: it fetches a row.
Maybe you were expecting the behaviour of mysqli_fetch_all?
Fetches all result rows as an associative array, a numeric array, or both
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I want to get data from database from specific row to last row.
How can I do it?
You can set limit and offset with your sql query like this.
SELECT *
FROM your_table
ORDER BY id DESC
LIMIT 1, 1;
See more : http://www.mysqltutorial.org/mysql-limit.aspx
$sql="Select * from table order by id desc limit 20";//It will give you last 20 records
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
What's wrong with this query and how can i output the result?
`SELECT sum(BM) from (SELECT rpdc.BM from rpdc LIMIT $start, $limit) as totalbm`
You need to give an alias for the sum something as
SELECT
sum(BM) as total_sum
from (SELECT rpdc.BM from rpdc LIMIT $start, $limit) as totalbm
Then in php you can use as
$row['total_sum'];
Hey you need to add as 'some_name' like SELECT name as firstname
Replcae with this sum(BM) as total
So, result array will treat total_sum as key of array.
You can use it as $result['total']
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am using Yii, I want to select all records of a model except the last 2 records, but I got an error that there is syntax error, here is my code:
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents) FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents))";
$model2 = Newsevents::model()->findAll($c);
This one considers, that idNewsEvents might not be sequential (due to deletes or whatever).
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents NOT IN (SELECT idNewsEvents FROM newsevents ORDER BY idNewsEvents DESC LIMIT 2)";
$model2 = Newsevents::model()->findAll($c);
P.S.: Not sure if this is correct yii syntax, I'm not familiar with it, but I'm very familiar with MySQL. Anyway, you get the idea I hope.
Two checking can be done in a single checking
$c->condition ="idNewsEvents < (SELECT MAX(idNewsEvents)-1 FROM newsevents)";
Example query
SELECT * FROM `users` WHERE user_id < (SELECT MAX(user_id)-1 FROM users)
it should be:
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents)
FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents)";
There were just an extra bracket at end and a very tired eyes to notice that :( .
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I have this query:
$sql = $link->query("SELECT m.id_rel, (SELECT SUM(visita) as total FROM icar_mas_vistas WHERE id_rel = m.id_rel), icar_categorias.nombre
FROM icar_mas_vistas AS m, icar_categorias WHERE m.id_rel = icar_categorias.id_categoria");
if($sql->num_rows){
while($row = $sql->fetch_object()){
echo ''.$row->total.' '.$row->nombre.'';
}
It's working great in the SQL console, but when printing in PHP the value of total is not showing, how can i print it?
Thanks
Try change query adding AS total after brackets, to:
SELECT m.id_rel,
(SELECT SUM(visita) as total FROM icar_mas_vistas WHERE id_rel = m.id_rel) AS total, icar_categorias.nombre
FROM icar_mas_vistas AS m, icar_categorias
WHERE m.id_rel = icar_categorias.id_categoria