PDA

View Full Version : PHP Question



Jack!
08-11-2012, 02:21 PM
Hi everyone, just a wee question.

I have been working on a small personal project for a while now, got the majority of it done, but this next bit is stumping me.

I have a basic MySQL Database and login system, Now I have a field in the Database called MABANNED, Now I was to do something like when they try and log in it checks against this field, If 1 show "You have been banned" and if 0 carry on loading the dashboard.

But, I've tried a few different methods and I cannot get it to work, any help?

Moved by Lee (Forum Super Moderator): From 'Tech Discussion'.

Tomm
08-11-2012, 02:25 PM
What methods have you tried?

Kieran
08-11-2012, 02:25 PM
$query = mysql_query("SELECT MABANNED FROM Table_Name WHERE username='TheirUsername'");

if($query = 1) { Show a banned message } else { Show the login area }

Put that in the area where it logs them in, so if they're banned, show the message, else process the login. I think that should work. If MABANNED isn't in the same table as their username, you'll have to join them together using IDs.

Tomm
08-11-2012, 02:26 PM
Don't really do much PHP anymore but I'm fairly sure you should not be using the old MySQL functions and be using PDO/MySQLi



$query = mysql_query("SELECT MABANNED FROM Table_Name WHERE username='TheirUsername'");

if($query = 1) { Show a banned message } else { Show the login area }

Put that in the area where it logs them in, so if they're banned, show the message, else process the login. I think that should work.

Kieran
08-11-2012, 02:33 PM
Don't really do much PHP anymore but I'm fairly sure you should not be using the old MySQL functions and be using PDO/MySQLi

Depends how he has done the rest.

Tomm
08-11-2012, 02:37 PM
Well obviously but there is no reason to be using the old MySQL functions unless you're working on a legacy project.


Depends how he has done the rest.

Kieran
08-11-2012, 02:38 PM
Well obviously but there is no reason to be using the old MySQL functions unless you're working on a legacy project.

Depends if he's new to it. I know the book I used which wasn't particularly old started you off on the legacy API. It didn't mention PDO until near the end of the book.

Tomm
08-11-2012, 02:49 PM
Sigh, this is why I don't like PHP and its whole ecosystem. Really there should not be 3 different ways of using MySQL in your project, but that is what you get from a poorly designed language that is trying to patch things up years from it initial creation. If the book was fairly new then it seems completely idiotic to encourage people new to the language to use an extension that is strongly not recommended for use any more and will, at some point, be deprecated and removed. Not like the procedural version of MySQLi is significantly more complicated.


Depends if he's new to it. I know the book I used which wasn't particularly old started you off on the legacy API. It didn't mention PDO until near the end of the book.

Jack!
08-11-2012, 03:01 PM
What I have learned has been threw together from guides, working things out for myself and what not, I'll try it, its in the same table so hopefully that will work, I get the feeling I have used many different types of PHP and MySQL throughout, but meh, its not for anything major.

Jack!
08-11-2012, 11:14 PM
Sorry for the double post, did this in the end:


$query = mysql_query("SELECT mabanned FROM `users` WHERE username='" . $username . "'");
$row = mysql_fetch_array($query);

if($row['mabanned'] > 0) {
die("You have been banned from logging in.");
}

Chippiewill
09-11-2012, 09:41 PM
$query = mysql_query("SELECT mabanned FROM `users` WHERE username='$username'");
$row = mysql_fetch_array($query);

if($row['mabanned'] != 0) {
die('You have been banned from logging in.');
}

Is a bit simpler.

In double quotes ( " ) you can reference variables directly. If you're not referencing variables use single quotes ( ' ) as they're more efficient in those circumstances.

Want to hide these adverts? Register an account for free!