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
Related
If I run a query that returns multiple rows, is there a way I can select just one row out of that result?
So if I do something like
SELECT * FROM table WHERE number = 10
and it returns 33 results, is there a way I can go through those one at a time instead of returning the whole result set at once, or just return, for example, row 5 of the result set?
I have read about scrollable cursors but it seems they don't work on MySQL, although that seems to be what I am looking for....
I am using PDO with MySQL and PHP. I hope this makes sense, if not I will try and explain better.
Edit: This worked for what I wanted. Thanks.
$stmt = $dbh->prepare("SELECT * FROM $table WHERE user_points = '$target' ORDER BY tdate DESC LIMIT $count,1");
is there a way I can select just one row out of that result?
Yes there is, you can use LIMIT:
SELECT * FROM table WHERE number = 10 LIMIT 1;
$sql= "SELECT * FROM $table WHERE user_points = '$target' ORDER BY tdate";
$stmt= $pdo -> prepare($sql);
$stmt->execute();
$data = $stmt ->fetchAll();
//You asked about getting a specific row 5
//rows begin with 0. Now $data2 contains row 5
$data2 = $data[4];
echo $data2['A_column_in_your_table'];//row 5 data
Well i have a query which gives me couple of records.
$sql = "SELECT * FROM tablename WHERE..... LIMIT $limit";//getting dynamic limit values.
This gives me.
$sql = "SELECT * FROM tablename WHERE..... LIMIT 61,10";
$res=parent::_executeQuery($sql);
$rs=parent::getAll($res);
return $rs;
Here total records found is 61, out of which only just 10 record is returning. I need to display the total record. i,e 61 in my HTML file.
How do i able to get the total records?
If you want to limit the result set using LIMIT you'll have to run a second separate query along the lines of "SELECT COUNT(*) FROM tablename WHERE...". Either that or just remove the LIMIT.
When I want to find out how many shoes Alfred has, I always count the rows in the table "usershoes" where the userid matches Alfred's
But since I switched to PDO, and select row count is not simple or bulletproof/consistent, I'm reconsidering my methods
Maybe I should instead keep an int field "shoes" directly in table "users", keep number of shoes there, and then increase/decrease that number for that user along the way? Feels not right..
If anyone has a solid method for simple row counting on an existing select query, without extra query, let me know
Try something like this
SELECT COUNT(*) FROM usershoes
WHERE userid="theIdOfTheUser";
I could not get count(fetchColumn()) or fetchColumn() to work correctly (outputted 1 when 0 was the real number)
So now I'm using this, and it works:
$sql = 'SELECT COUNT(*) as numrows, shoecolor FROM usershoes WHERE userid = ?'
$STH = $conn->prepare($sql);
$STH->execute(array($someuseridvar));
And then:
$row = $STH->fetch();
if ($row['numrows'] > 0) {
// at least one row was found, do something
}
With MySQL, you can use FOUND_ROWS():
$db = new PDO(DSN...);
$db->setAttribute(array(PDO::MYSQL_USE_BUFFERED_QUERY=>TRUE));
$rs = $db->query('SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 5,15');
$rs1 = $db->query('SELECT FOUND_ROWS()');
$rowCount = (int) $rs1->fetchColumn();
$rowCount will contain the total number of rows, not 15.
Taken from:
http://php.net/manual/en/pdostatement.rowcount.php#83586
Hi with the follow code I request what dates are all in my database without duplicates.
Then I save it to an array. In the array I also need an other value.
The value I need is how much users are in one day in the database without duplicates.
For Example the array must later lookslike 23.07.2013 - 10, 24.07.2013 - 50 (users).
I search for several hours but I don't find a good mysql query.
$query = "SELECT id, user, timestamp FROM stat WHERE timestamp BETWEEN '$datum1' AND '$datum2' GROUP BY timestamp";
$result = mysql_query($query,$db);
while($row = mysql_fetch_assoc($result))
{
mysql_num_rows($result);
$dataset1[] = array(strtotime($row['timestamp']),$number_of_users_on_this_day);
}
Try:
$query = "SELECT id, user, COUNT(*) as count FROM stat WHERE timestamp BETWEEN '$datum1' AND '$datum2' GROUP BY timestamp";
This will return the number of entries in the value 'count'
if you want distinct data, in place of * use
COUNT(DISTINCT id)
with whatever field you want to be unique in place of 'id'
this is my php code
$sql="SELECT * FROM table_name WHERE FROM_UNIXTIME(date, '%Y') = $q order by id desc";
$q = mysql_query($sql) or die(mysql_error().$sql);
$sql1="SELECT * FROM table_name WHERE FROM_UNIXTIME(date, '%Y') = $q";
$query=mysql_query($sql1);
this gathers the results correctly (everything is correct)
but when i use this to calculate the total results it gives me nothing, however i had for example 3 results:
$total = mysql_num_rows($query);
Because your input isn't a query handle... Since you set $q to the query handle, you should be using that in mysql_num_rows():
$total = mysql_num_rows($q);
As I can see $q is resource not a string value, for that reason $sql1 query would fail with error
If you ask for the number of rows before you've actually retrieved the rows, the database may return zero or some intermediate number. This is true for Oracle and MySQL (don't know about MSSQL but I suspect it's the same). From the PHP docs:
Note: If you use mysql_unbuffered_query(), mysql_num_rows()
will not return the correct value until all the rows in the
result set have been retrieved.
Even for a buffered query, PHP would have to fetch all the rows in order to count them.