Retrieving database info while requesting other data - php

I am not sure how to fix this issues.
Basicly, I need to be enable to retrieve data from a database.
But this needs to be limited to the users account plan.
For example: an user has a membership plan, which comes with 5 emails.
He will need to be enable to see 5 emails, instead of my whole email database getting extracted.
Code:
$SQLSelect = $odb -> query("SELECT * FROM `accounts` ORDER BY `ID` DESC LIMIT = '" . ($_POST['email']) . "'");

Desc limit is option what allows to display limited amount.
Example:
You have table with 5 rows and those rows have auto increment id.
wtih select * from users order by id desc limit 3, you will select users with id 1 , 2 , 3 because at begining you order it by id, so it select 1 ,2 ,3 ,4 ,5 and then u say that you need only 3 of them using desc limit. So it seems like you got desc limit wrong...
If i got your example then you need something like this :
$limit = 5;
query("SELECT * FROM `accounts` ORDER BY `ID` DESC LIMIT = ".$limit)
So here user will only see accounts with id 1 , 2 , 3 , 4 and 5.
If i didn't say what you need, i rly didn't get your question

Related

Fetch new posts of random user from mysql

I want to fetch and display posts of different users as they upload a post.. i can access their posts by userid but the posts are in sequence and not random or new posts. For example if 2 users have posted 4 times, 3 times first user and 1 time 2nd in this sequence 1st user post, 2nd user post, 1 user ,1 user, my query will return the 3 posts of user 1 first and then single post of user 2 irrelevant of the original sequence and time(old/new).
My query is
//this is my table -> user_post(userid,post_id,post,time)
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id'";
and i want to load only 4 latest posts and when i next refresh the field the next 4 posts must be loaded..
this is a Android Client-Server Based application and i'm using volley to make the http calls and exchange data using JSON.
Did you try to use the limit clause ?
You code would be like :
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id' LIMIT 4";
You need to include a LIMIT clause inside your query:
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id' LIMIT 0,4";
On every request you maintain a count of how many rows have loaded in $lastLoadedRow. The next several times you load you would do:
... LIMIT $lastLoadedRow,4;
... LIMIT $lastLoadedRow,4;
... LIMIT $lastLoadedRow,4;

PHP select random id from database

I want to select a random userid from this query where level = 1 (Normal user) and alive = Yes (User is alive and can play)
$getaid = $sql->query("SELECT userid FROM `users` WHERE `level`='1' AND `alive`='yes'");
I know I can use
$totalusers = mysql_num_rows($getaid);
$randomuserid = rand(1,$totalusers);
to select a random userid but in that code there's a chance that it select a user that is dead (alive = No) or a staff member (level >= 1). is there a better way I can select a random userid without a chance to grab staff members/dead members?
You can do
"SELECT userid FROM `users` WHERE `level`='1' AND `alive`='yes' ORDER BY RAND() LIMIT 1"
Instead of selecting all the records and trying to choose a random one in PHP's side, let the database do the heavy lifting for you:
SELECT userid
FROM `users`
WHERE `level` = '1' AND `alive` = 'yes'
ORDER BY RAND()
LIMIT 1
Your method is about the worst of all possible worlds -- you are bringing all the data back from the database to an array in PHP and then choosing a random value there. But, it should work, because you are only bringing back the appropriate users.
One way of doing this in the database that is simple enough is:
SELECT userid
FROM `users`
WHERE `level`='1' AND `alive` = 'yes'
ORDER BY rand()
LIMIT 1;
However, if you have more than a few hundred rows (or a few thousand), this starts to get too expensive. There are definitely other methods, but bringing all the data back is not a good idea.
One simple fix is to get approximately 100 rows and then randomize them:
SELECT userid
FROM `users` CROSS JOIN
(SELECT COUNT(*) as cnt FROM users `level` = '1' AND `alive` = 'yes') x
WHERE `level` = '1' AND `alive` = 'yes' AND
rand() < 100 * 1 / x
ORDER BY rand()
LIMIT 1;
For performance, you want an index on `users(level, alive).
You can use ORDER BY RAND() LIMIT 1:
$getaid = $sql->query("SELECT userid FROM `users` WHERE `level`='1' AND `alive`='yes' ORDER BY RAND() LIMIT 1");

mysql - find out if input exists in last 30 records

I want to know if the registering user's ip exist in the last 30 records. This will help me prevent multiple registrations. I have difficulty in my attempt to do this in mysql. This is where I'm at so far:
The old query that checks multiple ip's across all records:
$same_ip_count = mysql_num_rows(mysql_query("SELECT * FROM login_users WHERE ip='".$ip."'"));
The new code that fails:
$same_ip_count = mysql_num_rows(mysql_query(
"SELECT 'user_id'
FROM (
SELECT *
FROM login_users
ORDER BY user_id DESC
LIMIT 30
) AS t
WHERE t.ip = '".$ip."'"));
Kind of new to sql so any suggestions will be great at this point.
EDIT:
To be clear - I want to search only the last 30 records. I don't want to search all records and then limit them to 30.
First use a subquery to get the last 30 items. Then, you can select from that list of 30 where the IP matches.
Select * From
(SELECT *
FROM login_users
ORDER BY registration_date DESC
LIMIT 30) a
WHERE a.ip = '".$ip."'
Mysql doesn't know what are the 30 last records. If you use an auto-incrementing primary key then you can ORDER by that key in descendant order.
try this.
$query = mysql_query(
"SELECT 'user_id' as result from `login_users` WHERE ip = '".$ip."' ORDER BY user_id DESC LIMIT 30");
$same_ip = mysql_num_rows($query);

retrive second highest point from table with no unique column

i have one table named users. In this table i have one column named credits(not unique).
Now i want the second highest user accroding to the users credits. If the credits field is unique thenmy below query is working fine
SELECT * FROM users ORDER BY credits DESC LIMIT 1 , 1
But , if the users credit is not unique then its create problem for retrive me data
suppose,
mack has 200 credits
jack has 200 credits
rock has 150 credits
when i has this types of record then,in output of this query i want the rock record not jack
can anyone help me to find out the correct value ?
thanks in advance
SELECT a.*
FROM users a
INNER JOIN
(
SELECT DISTINCT credits
FROM users
ORDER BY credits desc
LIMIT 1,1
) b ON a.credits = b.credits
SQLFiddle Demo
Hope this helps (first get second highest credits then find the users having those that credit andselect one from the top`. This will retrieve one user having second highest credit):
SELECT * FROM users
WHERE credits = (SELECT distinct credits FROM users
ORDER BY credits DESC LIMIT 1,1)
LIMIT 1;
EDIT: If you also want to select within users having same score then use the appropriate filter/sorting condition e.g. to select rock between rock and jenni, you could have another ordering base on name(assuming name is the column having names)
SELECT * FROM users
WHERE credits = (SELECT distinct credits FROM users
ORDER BY credits DESC LIMIT 1,1)
ORDER name desc
LIMIT 1;
To get both rock and jenni, just remove the limit from the end and update the inner limit e.g:
SELECT * FROM users
WHERE credits = (SELECT distinct credits FROM users
ORDER BY credits DESC LIMIT 1, 1);
try this
Select * FROM users
Where Credits < (Select Max(Credits) From Users)
ORDER BY credits DESC LIMIT 1;

Simplest way to get the next Row from an SQL table when I know the second one

Hi everyone I am trying to write a sorting script
For this my user will click a move up button which posts the id of the current selection we want to move up to a new page where the script is processed.
So using the fetch functions below i am getting the sort id of the current row we want to move up
$sqlCurrent = "SELECT * FROM `contact` WHERE `contact_id` = $id LIMIT 0, 1 ";
$resultsCurrent= mysql_query($sqlCurrent) or die(mysql_error());
$rowC = mysql_fetch_row($resultsCurrent);
$currentSort =$rowC[9];
I then pulled out all the data in decending order using
Now if my current sort order is 6 I want to look for the row with the sort order with 3 or 4 or 5 in order so i used the decending order and the next sort scrip comes up in the next table.
$sql = "SELECT * FROM `contact` ORDER BY `contact`.`contact_sortOrder` DESC LIMIT 0, 30 ";
The question is how do I simply get data from that row using 1 or maybe 2 functions.
We cant simply look for the next sort order because it is possible it wont be there.
For this example i have used a database like this
rowId 1 Sort order 6
rowId 2 Sort Order 2
rowId 3 Sort Order 4
Now I am row Id 3 and want to replace it with the next one. So i need to pick up rowId 2 somehow using the shortest method.
Any help will be useful
Could be as simple as
$query = "
SELECT
x,y,z
FROM
contact
WHERE
contact_sortOrder < $currentSort
LIMIT
1
";
(assuming $currentSort is safe for "direct insertion" into the query. see http://docs.php.net/pdo.prepared-statements)
select prev.*
from contact as curr
left join contact as prev on prev.contact_sortOrder < curr.contact_sortOrder
where curr.contact_id = $id
order by prev.contact_sortOrder desc
limit 1

Categories