Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-09-2008, 09:02 PM Row won't delete
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
PHP Code:
require ("config.php");
$timeoutseconds 10// length of session
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;

   
$query mysql_query("SELECT ip FROM useronline WHERE timestamp<$timeout");
   while(
$row mysql_fetch_array($query)){
   
mysql_query("DELETE FROM users WHERE ip='".$row['ip']."'") or die(mysql_error());

please help! mysql_query("DELETE FROM users WHERE ip='".$row['ip']."'") isn't deleting anything or giving any mysql errors
MoForce is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-10-2008, 11:55 AM Re: Row won't delete
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
What are the errors your receiving?

From what I know, you cannot do a MySQL query into a query (In this case, the while() bit). Try the following:

PHP Code:
require ("config.php");
$timeoutseconds 10// length of session
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;

   
$query mysql_query("SELECT ip FROM useronline WHERE timestamp<$timeout");
   while(
$row mysql_fetch_array($query)){
      
$ips_to_remove[] = $row['ip']; // Store the IP.
      //mysql_query("DELETE FROM users WHERE ip='".$row['ip']."'") or die(mysql_error());
   

 
foreach(
$ips_to_remove as $value){ // Do the query here one after the other.
      
mysql_query("DELETE FROM users WHERE ip='".$value."'") or die(mysql_error());
      
usleep(5000000); // Wait 5 seconds, this stops you over loading your server.

__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 11-10-2008 at 12:01 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 11-10-2008, 04:05 PM Re: Row won't delete
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
Quote:
Invalid argument supplied for foreach()
is there a way to get the IPs into an array to fix that or is there another solution to this problem?
MoForce is offline
Reply With Quote
View Public Profile
 
Old 11-11-2008, 02:34 PM Re: Row won't delete
Extreme Talker

Posts: 246
Trades: 3
Quote:
Originally Posted by MoForce View Post
PHP Code:
require ("config.php");
$timeoutseconds 10// length of session
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;

   
$query mysql_query("SELECT ip FROM useronline WHERE timestamp<$timeout");
   while(
$row mysql_fetch_array($query)){
   
mysql_query("DELETE FROM users WHERE ip='".$row['ip']."'") or die(mysql_error());

please help! mysql_query("DELETE FROM users WHERE ip='".$row['ip']."'") isn't deleting anything or giving any mysql errors
Maybe the DELETE query was never running. You should echo your DELETE query to see if it is finding what you want. Is the mySQL timestamp field in the same format that the $timeout variable is?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 11-12-2008, 09:00 PM Re: Row won't delete
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
Quote:
Originally Posted by CouponGuy View Post
Maybe the DELETE query was never running. You should echo your DELETE query to see if it is finding what you want. Is the mySQL timestamp field in the same format that the $timeout variable is?
I echoed the query and it comes out perfect. I've found another way out of this problem that doesn't involve a query inside a query which does what I wanted to. Although, I still want to know if a mysql_query within a mysql_query is still possible and if so how? It would save a bunch of lines of code and would be awesome to use. Thanks for the help guys.
MoForce is offline
Reply With Quote
View Public Profile
 
Old 11-13-2008, 06:35 AM Re: Row won't delete
Extreme Talker

Posts: 246
Trades: 3
Yes, putting mysql queries inside a while loop should work. Also, you could put the query inside the same sql statement (I'm guessing you may have done this already).

SQL:
Code:
DELETE FROM users where ip IN (
   SELECT ip FROM useronline WHERE timestamp<$timeout
)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
CouponGuy is offline
Reply With Quote
View Public Profile
 
Old 11-13-2008, 07:37 AM Re: Row won't delete
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Quote:
Originally Posted by MoForce View Post
is there a way to get the IPs into an array to fix that or is there another solution to this problem?
That would suggest to me, that their are no IP's to delete. *Adds debugger

PHP Code:
require ("config.php");
$timeoutseconds 10// length of session
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;

   
$query mysql_query("SELECT ip FROM useronline WHERE timestamp<$timeout");
   while(
$row mysql_fetch_array($query)){
      
$ips_to_remove[] = $row['ip']; // Store the IP.
      //mysql_query("DELETE FROM users WHERE ip='".$row['ip']."'") or die(mysql_error());
   

 
if(
is_array($ips_to_remove ){foreach($ips_to_remove as $value){ // Do the query here one after the other.
      
mysql_query("DELETE FROM users WHERE ip='".$value."'") or die(mysql_error());
      
usleep(5000000); // Wait 5 seconds, this stops you over loading your server.
}} else {echo "Nothing to remove.";} 
Quote:
Originally Posted by CouponGuy View Post
Yes, putting mysql queries inside a while loop should work. Also, you could put the query inside the same sql statement (I'm guessing you may have done this already).

SQL:
Code:
DELETE FROM users where ip IN (
   SELECT ip FROM useronline WHERE timestamp<$timeout
)
I was thinking that, but the details are in different tables.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 11-13-2008 at 07:38 AM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Reply     « Reply to Row won't delete
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33905 seconds with 12 queries