Table Help/PHP

Blasteh

Lord Farquaad
Apr 3, 2013
1,151
513
So, I'm making a different type banlist in my housekeeping. I want the id (of the user) and username to display.

Where it says "rank" I am am going to make it say "Banned?" I want it echo 0/1 or No/Yes.

You must be registered for see images attach


It's multiple tables, so I'm pretty confused.. Any help possible is great.

Here's the code:
Code:
                            <div class="module-body">
                            <table class="table table-striped">
                            <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Username</th>
                                            <th>Rank</th>
                                        </tr>
                                    </thead>
<?php
$getUsers = mysql_query("SELECT id,username,rank FROM users");
 
while(($users = mysql_fetch_array($getUsers)))
{
echo "<tr><td>" . $users['id'] ."</td><td>" . $users['username'] . "</td><td>" . $users['rank'];
}
?>
 

Benden

maging ang maganda mamatay
Jun 4, 2010
2,281
1,480
Well, I don't know your database structure but I'm assuming

PHP:
$getUsers = mysql_query("SELECT id,username,rank FROM users");
to
PHP:
$getUsers = mysql_query("SELECT id,username,banned FROM users");

and

PHP:
echo "<tr><td>" . $users['id'] ."</td><td>" . $users['username'] . "</td><td>" . $users['rank'];
to
PHP:
echo "<tr><td>" . $users['id'] ."</td><td>" . $users['username'] . "</td><td>" . $users['banned'];

and that'll echo a 0 or 1, to make it say yes or no use a boolean
 

Blasteh

Lord Farquaad
Apr 3, 2013
1,151
513
Well, I don't know your database structure but I'm assuming

PHP:
$getUsers = mysql_query("SELECT id,username,rank FROM users");
to
PHP:
$getUsers = mysql_query("SELECT id,username,banned FROM users");

and

PHP:
echo "<tr><td>" . $users['id'] ."</td><td>" . $users['username'] . "</td><td>" . $users['rank'];
to
PHP:
echo "<tr><td>" . $users['id'] ."</td><td>" . $users['username'] . "</td><td>" . $users['banned'];

and that'll echo a 0 or 1, to make it say yes or no use a boolean
PlusEMU doesn't have a "banned" column in users (I don't think)
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
956
797
Did a sketch pdo because idek how to use regular mysql anymore
Code:
<?php
class PdoConnection {
    public static function dbConnect() {

            try {

                $dbh = ''
                $dbu = ''
                $dbp = ''
                $dbd = ''

                $conn = new pdo("mysql:host=$dbh;dbname=$dbd;", $dbu, $dbp);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $conn;
            } catch(PDOException $e) {
                die('fucking shit', $e);

            }
        }
}
?>


<?php
$query = PdoConnection::dbConnect()->prepare('SELECT id,username,rank FROM users');
$query->execute();
$data = $query->fetchAll();
foreach ($data as $row) {
            echo "<tr><td>" . $row['id'] ."</td><td>" . $row['username'] . "</td><td>" . $row['rank'];
}


//now for the bans, do you have a ban table in the db?
$query2 = PdoConnection::dbConnect()->prepare('SELECT id,username,rank FROM banned_users WHERE id < 6'); //replace banned users with your table and the rank, because idk.
$query->execute();
$data2 = $query2->fetchAll();
foreach ($data2 as $row) {
    echo "staf member banned: " . $row['username'];

}
?>
god that code block style is ugly^^
81aa841fdee842e39c5b63a5703c8a37.png
 
Last edited:

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Did a sketch pdo because idek how to use regular mysql anymore
Code:
<?php
class PdoConnection {
    public static function dbConnect() {

            try {

                $dbh = ''
                $dbu = ''
                $dbp = ''
                $dbd = ''

                $conn = new pdo("mysql:host=$dbh;dbname=$dbd;", $dbu, $dbp);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $conn;
            } catch(PDOException $e) {
                die('fucking shit', $e);

            }
        }
}
?>


<?php
$query = PdoConnection::dbConnect()->prepare('SELECT id,username,rank FROM users');
$data = $query->fetchAll();
foreach ($data as $row) {
            echo "<tr><td>" . $row['id'] ."</td><td>" . $row['username'] . "</td><td>" . $row['rank'];
}


//now for the bans, do you have a ban table in the db?
$query2 = PdoConnection::dbConnect()->prepare('SELECT id,username,rank FROM banned_users WHERE id < 6'); //replace banned users with your table and the rank, because idk.
$data2 = $query2->fetchAll();
foreach ($data2 as $row) {
    echo "staf member banned: " . $row['username'];

}
?>
god that code block style is ugly^^
d2f9fc71883d4680a930e3aa514e8ed7.png
Easier just to make a join tables query, instead of doing different 2 queries.
fetchAll then a foreach, you're not even executing the query? Normally you would do a $stmt->execute and then a foreach($stmt as $blabla){ } damn you're making this way more complicated than it actually is. But I feel ya, can't even do regular mysql_ anymore, its too complicated compared to OOP and way too shit that I bother help out people using mysql_

Sent from my SM-G928F using Tapatalk
 
Last edited:

griimnak

You're a slave to the money then you die
Jul 20, 2013
956
797
Easier just to make a join tables query, instead of doing different 2 queries.
fetchAll then a foreach, you're not even executing the query? Normally you would do a $stmt->execute and then a foreach($stmt as $blabla){ } damn you're making this way more complicated than it actually is. But I feel ya, can't even do regular mysql_ anymore, its too complicated compared to OOP and way too shit that I bother help out people using mysql_

Sent from my SM-G928F using Tapatalk
Yeah i got confused when i started writing the second query because i have no idea how the db is structured, however if op plans on doing the query in pdo i suppose you could use mine as a base.

And oh i know, very messy and inefficient. But fix it up boios.

oh and i just forgot to execute, fixed it now. See what python does to you?
 

Core

Member
Nov 10, 2016
356
138
Did a sketch pdo because idek how to use regular mysql anymore
Code:
<?php
class PdoConnection {
    public static function dbConnect() {

            try {

                $dbh = ''
                $dbu = ''
                $dbp = ''
                $dbd = ''

                $conn = new pdo("mysql:host=$dbh;dbname=$dbd;", $dbu, $dbp);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $conn;
            } catch(PDOException $e) {
                die('fucking shit', $e);

            }
        }
}
?>


<?php
$query = PdoConnection::dbConnect()->prepare('SELECT id,username,rank FROM users');
$query->execute();
$data = $query->fetchAll();
foreach ($data as $row) {
            echo "<tr><td>" . $row['id'] ."</td><td>" . $row['username'] . "</td><td>" . $row['rank'];
}


//now for the bans, do you have a ban table in the db?
$query2 = PdoConnection::dbConnect()->prepare('SELECT id,username,rank FROM banned_users WHERE id < 6'); //replace banned users with your table and the rank, because idk.
$query->execute();
$data2 = $query2->fetchAll();
foreach ($data2 as $row) {
    echo "staf member banned: " . $row['username'];

}
?>
god that code block style is ugly^^
81aa841fdee842e39c5b63a5703c8a37.png

Why would you want to open another connect if you already have an existing connection open? rip
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
956
797
Why would you want to open another connect if you already have an existing connection open? rip
The pdo connection? It's basically just to simulate what he wants to do in this instance, I wouldn't recommend using pdo & mysql together(obviously lol)
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
956
797
I know but should of gave him example in the old deprived mysql xD
- if your php still supports it (which is unlikely lol)
Honestly bro i don't remember how to use the regular mysql xD that's why i did this in pdo.
Haven't touched php in a long while, i've been on the python grind. So clean, so simple.
But when I did use php, i always used PDO. gotta have those prepared statements and object oriented binds
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
My SQL is rusty, and as I'm not 100% sure how PlusEMU works (database wise), but you can give this a shot:
PHP:
<div class="module-body">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Banned</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $getUsers = mysql_query("SELECT `users`.`id`, `users`.`username`, `bans`.`id` AS `ban_id` FROM `users` LEFT JOIN `bans` ON `bans`.`value` = `users`.`id` WHERE `bans`.`bantype` = 'user' AND `bans`.`expire` > UNIX_TIMESTAMP(NOW())");
          
            while (($users = mysql_fetch_array($getUsers))) {
                ?>
                    <tr>
                        <td><?= $users['id'] ?></td>
                        <td><?= $users['username'] ?></td>
                        <td><?= !is_null($users['ban_id']) ? "Yes" : "No" ?></td>
                    </tr>
                <?php
            }
            ?>
        </tbody>
    </table>
</div>
 

Users who are viewing this thread

Top