hello guys I have problem with mysql like and limit command when used together they return 0 null I tried this in my php then I go to test it inside phpmyadmin I get the same result here is the code
//mysql
SELECT * FROM `items` WHERE name like '%php%' LIMIT 9,9; //this return null
SELECT * FROM `items` WHERE name like '%php%' LIMIT 9 OFFSET 9; //this return null
SELECT * FROM `items` WHERE name like '%php%';//this return some results
//this is weird please help here is the php code that I use
//php
$db = DB::getInstance();
$results = $db->query("SELECT * FROM `items` WHERE name LIKE ? LIMIT {$start},9",array("'%{$search}%'"));//the query function does the bindvalue
//$start come form $_GET['page'];
//$start equation // $start = ($_GET['page'] * $perpage) - $perpage
//the $perpage is always 9 cause I want to display 9 always or less
//and for sure I check for them if they were set , empty , and escape them etc...
$results = $results->results();
printItems($results);//a function that print items in a special way
You do not say how many results you get when you have no limit set, but I assume it's less than 9?
The LIMIT 9,9 or LIMIT 9 OFFSET 9 sets a limit of 9 results total, but starts looking from result number 9. If there are less than 9 results, you will get 0 results returned. I'm guessing you have misunderstood the offset-part of the limit clause (which aren't needed).
Remove the OFFSET-part, and you'll get a result back, even with LIMIT present.
Ensure you have enough data that satisfy LIMIT 9, 9
LIMIT 9, 9 means retrieve rows from 10 to 18
Related
I am developing an android app where i want to fetch 5 records each time using this query.
SELECT *from contest_table WHERE created_by='$me' LIMIT 5;
Now i get 5 records , but i want to fetch more 5 (next) records from data base when i click on "FETCH-MORE-RECORD" button inside the app
You can use OFFSET :
SELECT * from contest_table WHERE created_by='$me' LIMIT 5,5;
This will return 5 more rows, from position the 6th row (6-10).
The first number is to declare the start position of the fetch, and the second one is two declare how many to fetch.
This could also be written like this:
SELECT * from contest_table WHERE created_by='$me' LIMIT 5 OFFSET 5;
You will have to give a limit start and range. It will look as follows:
SELECT *from contest_table WHERE created_by='$me' LIMIT 5, 5;
To get first 5 records
SELECT * from contest_table WHERE created_by='$me' LIMIT 0,5;
To get next 5 records
SELECT * from contest_table WHERE created_by='$me' LIMIT 6,5;
Like wise you can change the starting point increasing it by 5.
So here is the solution. You need to keep the track of the off. So delcare a static variable for store the off. also update the offset after every click so next time you get right result.
public static int offset=5;
Then use this query with your listener;
SELECT * from contest_table WHERE created_by='$me' LIMIT 5,5;
It's a simple logic, Which I am representing as PHP codes, You request for first page from android by including page 1. in URL
http://SITENAME.COM/index.php?task=articles&page=1
You grab the result coming from URL like here
$page = $_GET['page'];
$max = 5; //As you want to retrieve 5 results only
And here define $start from where to start retrieving values
$end = $page * $max;
$start = $end+1 - $max;
Your query will be like
SELECT * FROM table order by column desc limit $start, $max //start will be 1 and max value to retrieve will be 5
After that you request page 2 and URL will be
http://SITENAME.COM/index.php?task=articles&page=2
and again in the query $start will be 6 and max value to retrieve will be 5 that is $max
Hope this helps, This is what the logic behind pagination.
hello all i am a php developer and right now i am having a simple issue like i want to get the last 10 data out of my database table and then show random 5 out of them .
like see the code below.
$random=rand(0,18);
mysqli_query(
$connection,
"SELECT something1,something2
FROM `table`
where (id !='0')
ORDER BY time DESC
LIMIT 4 OFFSET $random");
this query selects last 4 updated fields randomely out of 18 but i want a something better solution to this
First, define "the last 10"; for now let's assume the last ten when ordered by time. So, that is in fact the first ten when I order them the order way (I guess you got that):
SELECT * FROM `table` WHERE (id != '0') ORDER BY time DESC LIMIT 10;
then we subselect that to randomize and limit:
SELECT * FROM (
SELECT * FROM `table` WHERE (id != '0') ORDER BY time DESC LIMIT 10;
) A ORDER BY RAND() LIMIT 5
Remember, your database is extremely powerful and fast, use it for any data-processing.
Not sure there is a great solution with Select. Your sample take 4 line in a row with an OFFSET which is not a truly random selection. You should limit your select to 10 and then get 5 row of the 10 by another php code.
$rows = mysqli_query($connection,
"SELECT something1,something2 FROM `table` where (id !='0')
ORDER BY time DESC LIMIT 10");
//then, get 5 random key from your array of row from the mysql select
$rand_keys = array_rand($rows, 5);
In case you prefer randomizing in PHP, you can use PHP's shuffle and array_slice.
<?php
$last_10_rows = mysqli_query($connection, "simple query to get last 10 rows");
shuffle($last_10_rows); // randomize the array
$random_5_rows = array_slice($last_10_rows, 0, 5); // take first 5
?>
For
When I run this query on phpmyadmin
SELECT *
FROM `product_stock`
WHERE `product_warehouse_id` =5
LIMIT 100000
it loads the data by section 50 by 50 till its stops. But when i do it in a normal php page it takes a while to load and sometimes i get server error. How can I aproach something similar to the way phpmyadmin loads the result?
http://www.tutorialspoint.com/php/mysql_paging_php.htm
this is tutorial how to add paging on mysql long queries.
The MySQL LIMIT takes a couple of possible arguments. A single value specifies to return all of those results to the caller. If you pass two values:
LIMIT 0, 50
Then you're passing the start row and the page size. The 10000 in your example is really just a short form for:
LIMIT 0, 10000
See the MySQL help documents on SELECT for some more detail.
I recommend if you're not going to use all data for all columns, do not use:
SELECT * FROM tablename WHERE 1 AND fieldkey = 'value'
If the data size is very large, causes performance problems in Mysql.
Use:
SELECT field1, field2, field3 FROM tablename WHERE 1 AND fieldkey = 'value'
In the SELECT statement, use comma separated names of the columns you want to display, this will help you the server to respond faster without problems and you can paginate through the results easyly.
Also verify that the data on "fieldkey" are indexed, this helps to the query to work faster.
You can paginate the results of your query with something like this:
php- Paginate data from array
This is what I wanted I had to do it with logic at the end it works, below the sample code:
while ($limit <= 1000){
$q1 = "SELECT *
FROM product_stock
WHERE product_warehouse_id = 5
LIMIT $start, $limit";
$r1 = mysql_query($q1) or die(mysql_error());
while($stock_info = mysql_fetch_assoc($r1)){
echo $stock_info[product_stock_id]."<br />";
}
$start = $limit + 1;
$limit += 50;
}
Hello everyone i would like to ask you why, when i put limit 0,5 and 5,10 it does not work correctly here is my code.
$sql = mysql_query("SELECT * FROM vn_questions WHERE published = 1 limit 0,5 ");
while($data=mysql_fetch_array($sql))
{
echo $data['author'];
}
it gives me 5 results but when i put
$sql = mysql_query("SELECT * FROM vn_questions WHERE published = 1 limit 5,10 ");
while($data=mysql_fetch_array($sql))
{
echo $data['author'];
}
it gives me 6 .. where is the problem and how Can i fix it? I tried +1 -1 but the output is not working correctly.
the syntax for MYSQL LIMIT is LIMIT OFFSET COUNT, so what your second query is saying is start at record 5, and show me 10 results. does your table only have 6 records in it? since that's less than 10 it will only show those 6 available.
From the SQL manual
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
AND
With one argument, the value specifies the number of rows to return from the beginning of the result set:
It was some time ago I worked with PHP, MySQL and SQL, so I need some help. In my table I have 44 rows, but I only want to get 24 of them. Before I have just loaded all the rows like in the code below, and now I need some help to modify it to only load 24 rows. Thanks!
$query = "SELECT * FROM {$tableObject} {$sort1};";
$res = $mysqli->query($query);
$row_cnt = mysqli_num_rows($res);
while($row01 = $res->fetch_object()) {
// Some other code here
}
Use this in your query:
LIMIT 24
LIMIT is a MySQL function that selects a particular range of results from your query results. There are basically two ways of using it:
By simply specifying the number of results you want to fetch, like LIMIT 24; or
By specifying another range in the form of LIMIT X, Y. Where X is the beginning and Y is number of rows you want to fetch, like: LIMIT 10,5 that would select the 5 results from row 11 to 15
In your particular case you can simply replace this line:
$query = "SELECT * FROM {$tableObject} {$sort1};";
For:
$query = "SELECT * FROM {$tableObject} {$sort1} LIMIT 24;";
or even:
$query = "SELECT * FROM {$tableObject} {$sort1} LIMIT 0,24;";
For a better understanding about how to use limit, I recommend you to read this page from MySQL manual