Page 1 of 1

SELECT script, where I would like to select active AND expir

Posted: Fri Dec 12, 2008 1:05 pm
by JKM
I'm sorry if this is question is regarding too mutch PHP, but a friend of mine said that I should make a better MySQL query that gets both expired bans AND active bans. I've tried, but for no help. Please move this thread if it's too mutch PHP.

ebid = expired ban id
bid = ban id

This script works fine where it's both active AND expired bans, but not if there is only expired bans.

Code: Select all

if(preg_match("^user_\d{1,10}$^",trim(strtoupper($_GET["s"])))) {
    mysql_connect('xxx','xxx','xxx');
    @mysql_select_db('xxx') or die("Unable to select database");
    $query = mysql_query("SELECT * from lol_old WHERE user_id='".$_GET['s']."'") or die(mysql_error());
    $query1 = mysql_query("SELECT * from lol_oldbans WHERE user_id='".$_GET['s']."'") or die(mysql_error());
    
    if(mysql_num_rows($query)) {
        while($myrow = mysql_fetch_array($query)) {
            if($myrow['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow['ban_length'].'min';
            }
            $result = ' <p>'.$myrow['ban_reason'].' ('.$length.') - <a target="_blank" href="/?bid='.$myrow['bid'].'">?bid='.$myrow['bid'].'</a> (ACTIVE)</p>'."\r\n";
        }
    }
    elseif(mysql_num_rows($query1)) {
    while($myrow1 = mysql_fetch_array($query1)) {
            if($myrow1['ban_length'] == 0) {
                $length = 'perm';
            }
            else {
                $length = $myrow1['ban_length'].'min';
            }
            $oldres .= '    <p>'.$myrow1['ban_reason'].' ('.$length.') - <a target="_blank" href="/ebid='.$myrow1['ebid'].'">?ebid='.$myrow1['ebid'].'</a></p>'."\r\n";
        }
    }
    else {
        $result = " <p>Not banned.</p>"."\r\n";
    }
}
else {
    $result = " <p>Error! The search has to start with 'user_'</p>"."\r\n";
}
echo $result;
echo $oldres;
As I wrote, I've tried to make a select code that selects both active and expired bans, but the page just keep loading and if I try to query directly with a query browser, it's not responding. Anyways, here is the select code I tried:

Code: Select all

SELECT * FROM lol_bans active, lol_oldbans expired WHERE active.user_id='".$_GET['s']."' OR expired.user_id='".$_GET['s']."'
Thanks for any help, but please, try not to send me to a guide - I'm totally blank!

Re: SELECT script, where I would like to select active AND expir

Posted: Fri Dec 12, 2008 2:05 pm
by SteveC
I don't think you can run a query like that. Maybe someone else here knows differently.

My thought would be that you should combine all of the bans into one table, and add an expiry ENUM to them with 'expired' or 'active' as the values.

Re: SELECT script, where I would like to select active AND expir

Posted: Fri Dec 12, 2008 3:20 pm
by JKM
Yeah, I know it would work - but unfortunately it won't be possible. But there got to be another way to solve this problem?

Re: SELECT script, where I would like to select active AND expir

Posted: Sat Dec 13, 2008 2:05 am
by SteveC
JKM wrote:Yeah, I know it would work - but unfortunately it won't be possible. But there got to be another way to solve this problem?
I think that MySQL has a UNION command that would allow you to run two queries in one. Might be worth researching that.

Although it'd still require just as much work for MySQL?