so I execute query:
SELECT SQL_CALC_FOUND_ROWS * FROM table l LIMIT 10, 20;
which returns 20 rows from table, and there are a total 553 rows in the table
Then i immediately execute SELECT FOUND_ROWS();
But this instead only returns the number 1, despite the fact that there are 553 rows in my table (it's supposed to return 553, am I correct?)
what did I do wrong?
I suspect you have a syntax error, as names in SQL are not supposed to contain spaces. Try adding square brackets around [table 1], if that is the name of your table.
Remove your limit, I think there it is not showing 20 rows; it is showing only 10 rows.
$sql = "SELECT SQL_CAL_FOUND_ROWS * FROM users "; //don;t write table then table name
$result = mysql_query($sql);
//Use these according to your requirements:
$sql = "SELECT SQL_CAL_FOUND_ROWS * FROM users ";
$result = mysql_query($sql);
$sql = "SELECT FOUND_ROWS() AS `found_rows`";
$rows = mysql_query($sql);
$rows = mysql_fetch_assoc($rows);
$total_rows = $rows['found_rows'];
Related
I am using PHP, Silex, and a PostgreSQL database. I have a piece of code that executes exactly as expected:
$statement = $app['pdo']->query("SELECT * FROM my_table LIMIT 50");
$string = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($string);
return $json;
When I change one thing by adding a "WHERE" parameter, it fails:
$statement = $app['pdo']->query("SELECT * FROM my_table LIMIT 50 WHERE id > 3000");
$string = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($string);
return $json;
I checked that "id" is not capitalized in the schema and it is formatted as an integer. Id values in the table range from 1 to 5000, and it is populated with random entries, many of which should satisfy the query.
What is going wrong?
WHERE comes first
SELECT * FROM my_table WHERE id > 3000 LIMIT 50
Limit goes after the where clause
$statement = $app['pdo']->query("SELECT * FROM my_tableWHERE id > 3000 LIMIT 50");
I've got a site with a PHP script, this script has an SQL query inside returning data that is accessed by a JavaScript file. The data is a huge list of flight data, and I need to be able to select (let's say) a random 40% of the total flights for any given day specified. For arguments sake lets put it like this:
$query = "SELECT * FROM `Flight_Data` WHERE DepDateTimeUTC LIKE '%1/1/14%' ";
I understand that to get a random number of rows you simply use ORDER BY RAND() LIMIT 40' and ideally I want to say LIMIT 40% but that doesn't work.
EDIT:
$query = "SELECT * FROM `Flight_Data` WHERE DepDateTimeUTC LIKE '%1/1/14%' ";
$row = mysqli_fetch_row($result);
$total = $row[0];
$percent = $total * 0.40;
$query = "SELECT * FROM `Flight_Data` WHERE DepDateTimeUTC LIKE '%1/1/14%' LIMIT . $percent ";
You can COUNT all records and then calculate the % you need like this:
$query = "SELECT COUNT(*) FROM `Flight_Data` WHERE DepDateTimeUTC LIKE '%1/1/14%' ";
$result = mysqli_query($connection,$query);
$row = mysqli_fetch_row($result));
$total = $row[0];
$percent = intval($total * 0.40);
$query = "SELECT * FROM `Flight_Data` WHERE DepDateTimeUTC LIKE '%1/1/14%' LIMIT ". $percent;
//execute your query....
Since you are using a php script you should be able to achieve what you want. What you can do is, get the total no of rows in the table, which goes like:
SELECT count(*) AS Total FROM Flight_Data
With php you can calculate the 40% of that total.
Lets say $myPercent contains the calculated 40% of total. You can use the value of $myPercent in the limit as
$query = "SELECT * FROM `Flight_Data`
WHERE DepDateTimeUTC LIKE '%1/1/14%'
LIMIT ".$myPercent;
Here, we escape the php variable from the mysql query string which is a good practice.
In mysql, % is used as wildcard, so it cannot be used like you thought you could in limit.
Hope this has helped you.
When I run the following query in Phpmyadmin it returns 10 records, like it should
SELECT * FROM sku WHERE sku LIKE '142-401-117-282%'
But when I run the same query in my Php script, I get all the rows from the table
$sql = "SELECT * FROM sku WHERE sku LIKE '142-401-117-282%' ";
$result = $con->query($sql);
while($row = mysqli_fetch_array($result)){
echo $row['sku']."<br>";
}
I'm selecting all the SKU's that start with 142-401-117-282, after come a - plus the size of the item
How do I get the 10 rows that I need?
Try:
$sql = "SELECT * FROM sku WHERE sku LIKE '142-401-117-282%' LIMIT 0,10";
where 0 is starting row position and 10 is its length.
I'm using the following query to display some information:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
My issue is it works fine when I leave out ORDER BY count DESC but when it is there I get the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /proj/co600/project/repo/public_html/select_field3.php on line 227
Count is a column in my database which records the number of times a publication is downloaded.
count is an aggregate function, so you need to surround it with backticks.
To get a clear cut picture of your error.. You need to change your code like..
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY count DESC ");
if(!$result)
{
die(mysqli_error($con));
}
You are having MySQL reserved keyword as column name in table.
Use Below query:
$result = mysqli_query ($con,"SELECT * FROM files,members,member_group WHERE files.member_id = members.member_id AND members.member_id = member_group.member_id AND group_id='$id' ORDER BY `count` DESC ");
I'm trying to select values from row 85 in this while loop.
So all values that are selected in the database before row 85 should be excluded in the while loop, and everyone above 85 should be "Do Something" with.
Any suggestions how to achieve this?
$to_emails = mysql_query("SELECT * FROM ".$DBprefix."users WHERE workouts > 10");
while ($to_email = mysql_fetch_array($to_emails)) {
// Do Something
}
Look here:
To retrieve all rows from a certain offset up to the end of the
result set, you can use some large number for the second parameter.
This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
http://dev.mysql.com/doc/refman/5.0/en/select.html
So try something like this:
$to_emails = mysql_query("
SELECT * FROM ".$DBprefix."users
WHERE workouts > 10
LIMIT 85,18446744073709551615");
Try like
$to_emails = mysql_query("SELECT * FROM ".$DBprefix."users WHERE workouts > 10 LIMIT 85,200");
Or even try like(May be it works)
$to_emails = mysql_query("SELECT * FROM ".$DBprefix."users WHERE workouts > 10 LIMIT 85,(SELECT COUNT(*) FROM ".$DBprefix."users");