I have a live chat and i need to ban if the users chatted more than 5 times in a row
this is the sql:
function countMessages() {
global $tsUser;
$querys = db_exec(array(__FILE__, __LINE__), 'query', 'SELECT * FROM c_chat_messages where msg_user = "'.$tsUser->uid.'" ORDER BY msg_id ASC');
$counts = db_exec('num_rows', $querys);
if($counts > 5) { $this->banUser(); }
}
$tsUser->uid is the id of the user
i need to check if the user chatted for more than 5 times in a row so the php executes the $this->banUser(); function
you have to take timestamp of each message sent by the user in your database table.
And than you can fire a query checking if the user has sent more than 5 messages in last 15 seconds, by calculating the time different between the messages.
to get last five messages sent by that user,
SELECT * FROM c_chat_messages where msg_user = "'.$tsUser->uid.'" ORDER BY msg_id DESC limit 0,5
than get the difference between last and first record returned by above query.
to get time difference in seconds you can use
SELECT TIME_TO_SEC(TIMEDIFF('2010-08-20 12:01:00', '2010-08-20 12:00:00')) diff;
you can call a ajax file every one or two seconds to check this.
Let me know if further clarification needed.
Related
I have this database of events and a function in PHP that logs/insert data into it every 1 hour which is either HOT or COLD and another function that inserts a record every minute just need to insert a couple of different data but when it comes to displaying it, it should just get the last status of the record if it's HOT or COLD.
so basically the behavior is like below, the every-minute frequency has no status data so I need to get the status from the last record that has a status or is not empty.
ID
Status
Frequency
1
COLD
every minute
2
COLD
every minute
3
COLD
every minute
4
COLD
every minute
5
COLD
hourly
6
HOT
every minute
7
HOT
every minute
8
HOT
every minute
9
HOT
every minute
10
HOT
hourly
I tried the below query to get the last record that is either HOT or COLD but it was applied on every record inside the loop
while($row = mysqli_fetch_assoc($result)){
if($row['status'] == "HOT"){
echo "HOT";
}else if($row['status'] == "COLD"){
echo "COLD";
}else{
//if the record status is not equal to HOT or COLD
$hourly_record = get_last_record();
echo $hourly_record['status'];
}
}
the get_last_record() function
function get_last_record() {
$sql = "SELECT * FROM events WHERE status = 'HOT' OR status = 'COLD' ORDER BY ID DESC LIMIT 1";
//some execution
return $rows;
}
But the output is once it gets a status of either HOT or COLD all of the records will share the same status, is there a way on MySQL or PHP to get the next record with a different status?
Change your select query with this query:
SELECT * FROM events WHERE DATALENGTH(status) > 0 ORDER BY ID DESC LIMIT 1
I have a table with with columns name:
id
message
sender
chat_id
I want to get one row from number of rows with same chat_id ,during while loop fetch.
What Will be the MySQL Query
If you only want one row, one of those columns needs to have unique data so you can add it to the WHERE clause
SELECT id,message,sender,chat_id from information WHERE id=2 for example.
SELECT id,message,sender,chat_id from information WHERE chat_id=5 AND sender='mark'
OR during the while loop, match the column and grab whatever you want from it.
But again, it needs to be a unique combination of data otherwise it'll be overwritten by the previous.
while($row = $result->fetch_assoc()){
if($row['sender'] == 'mark' && $row['chat_id'] == '5'){
$thisIsMyRow = $row['id'];
}
Please could you explain more? What is it you're trying to achieve?
Say this is your query:
SELECT id,message,sender,chat_id from messages WHERE chat_id=1
You may have 200 rows that match that query. To return just one result, you can do a couple of things.
If you want the latest message, you can do this:
SELECT id,message,sender,chat_id from messages WHERE chat_id=1 ORDER BY id DESC LIMIT 1
another example, latest 5 messages
SELECT id,message,sender,chat_id from messages WHERE chat_id=1 ORDER BY id DESC LIMIT 0,5
In addition when you take it back into PHP, rather than going through the loop, you can JUST do this:
$row = $result->fetch_assoc()
As our query is only returning 1 row, you don't need to loop through. You can do this when your query returns multiple results - but it'd be better to optimise your query.
I can advise better if you are able to elaborate.
while($row = $result->fetch_assoc()){
$query = "SELECT id, message, sender,chat_id FROM messages WHERE Y = X AND Z = $K LIMIT 1";
}
replace the WHERE part with whatever column you want
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.
I am trying to get the highest value of an auto_incrementing id from a returned MYSQL query.
Essentially I have returned the last 10 records from a table called chat. these are user-submitted chat messages each assigned an auto_incremented id.
So if I return the last 10 messages to be displayed in a chat window I then intend to use AJAX to load a php script (possibly every second) that queries the database again. This time to display all chat messages which have an id greater than than the highest previous id so the chat auto-refreshes.
I intend to store the highest 'id' from the first chat block in a $_SESSION["latest_chat_id"]. I will then query mysql ... select * from chat where id > '{$_SESSION["latest_chat_id"]}'.
Return the MYSQL result and update the $_SESSION["latest_chat_id"] from the new returned data.
Thereafter recalling this script every second to keep updating the chat window with new chat messages.
My question is how do I get the value of the highest id form the first result set. My code is below...
<?php include("../database/connect.php");
$query1 = "SELECT * FROM chat ORDER BY message_id DESC LIMIT 10";
$result1 = mysqli_query($connection, $query1); ?>
<div class="chatbox">
<ul>
<?php
while($chat_row = mysqli_fetch_assoc($result1)) { ?>
<li><?php echo $chat_row["message_id"] . " " . $chat_row["message"] . " " . $chat_row["message_test"] ; ?></li>
<?php
}
?>
Should I do another query such as...
$query = "SELECT message_id FROM chat ORDER BY message_id DESC LIMIT 1";
Or can I access this value from the first returned data set?
Thanks,
Rob
I have insert in table any time when users open any post on my site, in this way im get real time 'Whats happend on site'
mysql_query("INSERT INTO `just_watched` (`content_id`) VALUES ('{$id}')");
but now have problem because have over 100K hits every day, this is a 100K new rows in this table every day, there is any way to limit table to max 100 rows, and if max is exceeded then delete old 90 and insert again or something like that, have no idea what's the right way to make this
my table just_watched
ID - content_id
ID INT(11) - AUTO_INCREMENT
content_id INT(11)
Easiest way that popped into my head would be to use php logic to delete and insert your information. Then every time a user open a new post you would then add the count the database. (this you are already doing)
The new stuff comes here
Enter a control before the insertion, meaning before anything is inserted you would first count all the rows, if it does not exceed 100 rows then add a new row.
If it does exceed 100 rows then you before inserting a new row you, first do a delete statement THEN you insert a new row.
Example (sudo code) :
$sql = "SELECT COUNT(*) FROM yourtable";
$count = $db -> prepare($sql);
$count -> execute();
if ($count -> fetchColumn() >= 100) { // If the count is over a 100
............... //Delete the first 90 leave 10 then insert a new row which will leave you at 11 after the delete.
} else {
.................. // Keep inserting until you have 100 then repeat the process
}
More information on counting here. Then some more information on PDO here.
Hopefully this helps :)
Good luck.
Also information on how to set up PDO if you haven't already.
What I would do? :
At 12:00 AM every night run a cron job that deletes all rows from the past day. But thats just some advice. Have a good one.
Use this query for deleting old rows except last 100 rows:
DELETE FROM just_watched where
ID not in (SELECT id fromjust_watched order by ID DESC LIMIT 100)
You can run it by CRON in every n period where (n= hours, or minutes, or any)
$numRows = mysql_num_rows(mysql_query("SELECT ID FROM just_watched"));
if ($numRows > 100){
mysql_query("DELETE FROM just_watched LIMIT 90");
}
mysql_query("INSERT INTO `just_watched` (`content_id`) VALUES ('{$id}')");
I guess this should work fine.
You can get the number of rows in your table with:
$size = mysql_num_rows($result);
With the size of the table, you can check, if it's getting to big, and then remove 90 rows:
// Get 90 lines of code
$query = "Select * FROM just_watched ORDER BY id ASC LIMIT 90";
$result = mysql_query($query);
// Go through them
while($row = mysql_fetch_object($result)) {
// Delete the row with the id
$id = $row['id'];
$sql = 'DELETE FROM just_watched
WHERE id=$id';
}
Another way would be to just delete an old row if you add a new row to the table. The only problem is, that if something get's jammed, the table might get to big.
You may use
DELETE FROM just_watched ORDER BY id DESC LIMIT 100, 9999999999999999;
So, it'll delete all the rows from the offset 100 to a big number (for end of the tables). if you always run this query before you insert new one then it'll do the job for you.