Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to obtain the last value that I put in.
I am trying to obtain the number on which my article is. So I use this query:
$totalPages= mysql_query("SELECT page FROM `easy_db`.`article` ORDER BY article_id DESC LIMIT 1") or die(mysql_error());
The value that I get is 6.
Why 6? I truncated the table..and tried again and I get 6 again.. It should be 1.
Another question, I want to enforce types in php..
I try to enforce an Int, but it doesnt seem to work:
$page=(int) $totalPages/10;
mysql_query returns a resource handle, not the result of the query itself. See the PHP manual for some basic examples of how to query a database. For example:
$result= mysql_query("SELECT page FROM `easy_db`.`article` ORDER BY article_id DESC LIMIT 1") or die(mysql_error());
if ($row = mysql_fetch_assoc($result)) {
$totalPages=$row['page'];
}
With regard to your second question, pay attention to the operator precedence rules - the (int) operator has a higher precedence than division, so is carried out before the division. Thus, you can get a floating point result. Alternatively try one of these
$page = (int)($totalPages/10); #rounds down
$page=round($totalPages/10); #rounds up or down as appropriate
Also checkout floor() and ceil() as alternatives to round, as they are often useful when writing pagination calculations.
You can use mysql_insert_id() function in php to retrieve the id of the last inserted row.
mysql_insert_id
You should use mysql_insert_id(); immediately after your insert statement.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
please how can i query the database table one row at a time without repeating the same row until all the rows has been selected.
E.g
I want the query to select the first row the first time and second row the second until all the rows is selected. Note i don't want to use random selection has result may repeat itself.
Thank you in advance
mysql has an OFFSET option that will work perfectly for what you want to (not that i understand why you are doing what you want to do) do. eg
$offset = 0;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;
$offset++;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;//offset is now 1
$offset++;
$query = 'SELECT * FROM my_table LIMIT 1 OFFSET='. $offset;//offset is now 2
I think in your case you would chuck it in a loop. I'm just showing it as above to illustrate the concept. Or maybe you will pass $offset as a parameter. Either way this should give you a good idea
What does offset do? It basically ignores the records before the offset count. so if i have records 1,2,3,4,5 and I select from this list with an offset of 2 I will get 3,4,5 as my list.
W3 Will explain it better than me
So this question is regarding an API request and response
Your response to the first api request should contain last_offset. Tell your consumer to pass offset as a parameter on their request. my.api.com?offset=1 after validation and sanitation of the parameter offset, you can include the value in your query else use default offset of 0
If they received last_offset=2 in the last response they should be passing offset=3 on the next request.
One way is make the primary key of the table a sequence of numbers starting from an arbitrary number.
Get hold of the starting index.
While writing the sql query,just write a loop and make the where=id + 1.This will give you each row at a time.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info
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
Ok, I've got a quick question. I'm using PDO to communicated with a MySQL db. Using BETWEEN with two dates as the variable is easy:
$db->query("SELECT * FROM awesome WHERE date BETWEEN :start AND :end");
What I don't think is easy is when I have one date and two columns. This doesn't work, no matter how loudly I cuss:
$db->query("SELECT * FROM awesome WHERE :one_date BETWEEN start_col AND end_col");
Is there a way to use BETWEEN without reverting to something awful like...?
$db->query("SELECT * FROM awesome WHERE '$one_date' BETWEEN start_col AND end_col");
Or should I just stick to not using BETWEEN in this case?
$db->query("SELECT * FROM awesome WHERE start_col<=:one_date1 AND end_col>=:one_date2")
Thanks!
BETWEEN only works with one column and two values.
But your last approach should work. That's at least the way to go
$db->query("SELECT * FROM awesome" .
" WHERE start_col <= :one_date1 AND end_col >= :one_date2"
);
When using named parameters you should also be able to use the same name twice and only need to bind once.
Update
Well, just tested it and worked for me with " WHERE 'value' between col1 and col2". (tested postgres with datetime, mysql with integer)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get best time from MySql table collumn which I made varchar (I can change it, just I didn't know better) which data looks like "00:05:22.22" (it's "hours:minutes:seconds.milliseconds").
it's like laps times in that collumn..
or I should change my DB table structure? to what?
and how I should do it then?
i.e.
in that collumn are records like this:
00:00:04.99
00:00:04.57
00:00:04.55
00:00:04.58
00:00:03.36
And I would like to get all of them ordered by shortest time. Like best lap.
If you are using MySQL 5.6.4 or greater, you can use the TIME datatype for your column and indicate that it should accept fractional parts of a second - see here.
Otherwise, using a VARCHAR column, you can still use the MIN/MAX functions for your data and expect to get a valid result (provided all values are correctly formatted), since alphabetically sorting the data in your use case should give the same result as numerically sorting it.
Alternatively, you can use the ORDER BY clause on that column and take just the first n results. Depends on your needs.
To get an ordered set from a table your query needs to specify an order -
SELECT *
FROM `table`
ORDER BY `lap_time` ASC
You do not have to include ASC in the query as any ORDER BY will sort from smallest to largest unless otherwise told not to do so with DESC
As you have said, your column datatype is varchar, you can do
select [column_names] from [table_name] order by [column_name_containing_time] asc
You can see an example here: SQL Fiddle
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am creating my first ever project on the topic "online mobile store" on NETBEANS IDE using PHP and MySQL as backend,
on a page products.php I want to display 1st 10 products, then by clicking on NEXT button next 10 products will display....
Please tell me how to do this?
You need to use the LIMIT sentence on the Mysql query, like this:
SELECT * FROM table LIMIT 0,10
The first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return
You will need to increase by 10 the first number using PHP.
For example, if you want the second page (from 10 to 20) your query should be:
SELECT * FROM table LIMIT 10,10
https://dev.mysql.com/doc/refman/5.0/en/select.html
First of all, PHP and MySQL are both backends. HTML/CSS/JS is for frontend.
Second, the thing is achieved by altering your query using LIMIT.
For first ten : mysql_query(".... LIMIT 0, 10");
For second ten: mysql_query(".... LIMIT 10, 20");
... and so on
Where you get the START and OFFSET from your URL like this:
example.com/article.php?page=0 -> LIMIT 0, 10
example.com/article.php?page=1 -> LIMIT 10, 20
I leave the algorithm to you ;)