Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have made a log in system which asks for email activation. It all works fine. However, there is no real 'spam' protection. When the user creates an account, it inserts a row with the column 'activated' = 'NO'.

I want to make it so that if the user hasn't activated their account with, let's say, 3 hours, it will delete the row from the MySQL database. I have made a cron job which runs the following every 30 mins.

<?php

ob_start();
include 'global.php';
ob_end_clean();

$check_times = (time() - ('-3 hours'));
$query = "SELECT * FROM users WHERE time<=$check_times and activated='NO'";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$inactive_user = $row['username'];

echo $inactive_user;
echo "<br>";
echo $check_times;
?>

The 'global.php' is connecting to the database. The above code works but only for one row (IT ONLY RETURNS THE VALUE OF THE FIRST INSTANCE IT COMES ACROSS)

I have looked into 'Do While' and 'Loop Until' and whatever but don't know what to put in the brackets...Do While (//what do I put in here?)

At the moment, the code only echos out the instances but I will change that to delete

share|improve this question
 
Possible duplicate: stackoverflow.com/questions/3137992/… –  showdev Jun 6 '13 at 17:14
add comment

2 Answers

up vote 0 down vote accepted

You insert all the usernames inside $inactive_user[].

    $inactive_user = array();
while($rows = mysqli_fetch_array($result){
    $inactive_user[] = $rows['username'];
}

And then loop the array and echo every user:

foreach($inactive_user as $us){
    echo $us;
}

As mysql is deprecated, you should use mysqli or PDO

share|improve this answer
 
Brilliant, works a treat. However, it doesn't seem to like mysqli. Is there a reason for this and how important is it? –  Josh-Mason Jun 6 '13 at 18:00
 
Sorry...brain freeze. How exactly would I delete them from the table? I was thinking something like: foreach($inactive_user as $us){ mysql_query ("DELETE * FROM users WHERE username = $inactive_user "); } –  Josh-Mason Jun 6 '13 at 18:06
 
$us is one string and it wouldn't recognise them as different users and $inactive user doesn't work –  Josh-Mason Jun 6 '13 at 18:08
 
mysql_query("DELETE FROM users WHERE username = '$us'"). Note that you need to set in your TABLE users an UNIQUE INDEX to avoid duplicated usernames. mysql will not be supported in the future. mysqli is the "new updated" mysql. If you want to query with mysqli you have to set your connection with mysqli. E.g. $link = new mysqli($host, $user, $password, $database); php.net/manual/en/mysqli.query.php If my post answered your question, please accept it by click on the check. –  Perocat Jun 6 '13 at 18:31
add comment
while($row = mysql_fetch_array($result)) {
    $inactive_user = $row['username'];
}

Additionally, you should phase out the use of mysql_ functions, and look into mysqli as a replacement - mysql_ is deprecated.

share|improve this answer
 
I get a lot of errors for line 9 - $result = mysqli_query($query) or trigger_error(mysqli_error().$query); –  Josh-Mason Jun 6 '13 at 17:50
 
I can't write all the errors but it's expecting at least 2 parameters on line 9 and I get this 'Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given' –  Josh-Mason Jun 6 '13 at 17:53
 
It's not a swapout of functions - there's a bit more to the transition but you absolutely should look into it - perhaps start here. –  jterry Jun 6 '13 at 17:53
 
NOPE, SORRY...I CHANGED IT ALL BACK TO mysql_... instead of mysgli_... –  Josh-Mason Jun 6 '13 at 17:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.