Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[HELP] MySQL & PHP

Junior Spellweaver
Joined
Aug 14, 2008
Messages
146
Reaction score
12
Alright, so I'm trying to setup a page where it displays how many points I have.

$ucoins = mysql_query("SELECT votingpoints FROM accounts WHERE `name`='".$i['name']."");
I have my query, but it keeps turning out as "0".

What I want is it to say "You have 1 U-Coin(s)".

So what I tried to do is this:
echo"You have " + $ucoins " U-Coin(s)"; -- this gave me a zero that didn't follow HTML code (<br>'s and what not didn't work)

The biggest problem I'm having is formatting it, and getting the users username. The user would have to be logged in. How would I check what account the user logged in with?

Thanks :)
 
Joined
Nov 17, 2008
Messages
800
Reaction score
1,392
Well for starters...

Code:
echo"You have " + $ucoins " U-Coin(s)";

is not good syntax.

It would be:

Code:
echo "You have " . $ucoins . " U-Coin(s).";

PHP uses periods for concatenation.


As far as getting the user's username, by the looks of your query, you're using their username to look them up in the database, so simply

Code:
echo $i['name'];

should give you their username.
 
Junior Spellweaver
Joined
Aug 14, 2008
Messages
146
Reaction score
12
Ah, sorry the +'s are from Java :p.

I'm fairly new to PHP. Thanks a lot, I'll try it now :D!

Edit: The $i didn't seem to be anything. I took that out of my Login script. I need to find the session's username. This is so confusing for me :S, I don't really know where to start. I could do it, only if the player inserted their username. If anyone has ideas, throw em at me :)
 
Last edited:
Joined
Aug 4, 2010
Messages
572
Reaction score
177
Trying creating queries , one for accessing the database then following upon that a query to select the specific rows from your table and in a while loop to gather the info.. heres a example since you couldn't seem to post any script.

Code:
<?php
mysql_connect('','','');
mysql_select_db('','');

$yourquery = mysql_query("SELECT columnname FROM tablename WHERE id = '$_SESSION[user_id]'");
while ($row = mysql_fetch_assoc($yourquery){

echo $row['columnname']; //This will output the data UCoins just make a sentence to it as in , You have 1 UCoins.. There is a way simpler way to write this I just chose this 

}


?>
 
Initiate Mage
Joined
Aug 14, 2009
Messages
22
Reaction score
16
The biggest problem I'm having is formatting it, and getting the users username. The user would have to be logged in. How would I check what account the user logged in with?

I don't think you even initialized a session variable for this. Go google $_SESSION and learn instancing from it.

You can also use {} or curly braces when inserting a variable inside a string. Example: echo "You have {$ucoins} U-Coin(s)";
 
Junior Spellweaver
Joined
Aug 14, 2008
Messages
146
Reaction score
12
Alright, the mysql_query and query_row I understand how to use. For session variables, I will post my login script.

PHP:
<?php 
if(isset($_SESSION['id'])){
	echo "<meta http-equiv=refresh content=\"0\" />";
}else{
	if(isset($_POST['login'])) {
		$u = mysql_real_escape_string(stripslashes($_POST['username']));
		$p = mysql_real_escape_string(stripslashes($_POST['password']));
		$s = mysql_query("SELECT * FROM `accounts` WHERE `name`='".$u."'") or die(mysql_error());
		$i = mysql_fetch_array($s);
		
		if($i['password'] == hash('sha512',$p.$i['salt']) || sha1($p) == $i['password']){
			$userz = mysql_query("SELECT * FROM `accounts` WHERE `name`='".$i['name']."' AND `password`='".$i['password']."'") or die(mysql_error());
			$auser = mysql_fetch_array($userz);
			$_SESSION['id'] = $auser['id'];
			$_SESSION['name'] = $auser['name'];
			$gmammt = $gmlevel - 1;
			if($auser['gm'] > $gmammt){
				$_SESSION['gm'] = $auser['gm'];
			}
		} else {
			$return = "Invalid username or password.";
		}
	}
		echo "
		<div class=\"regtext\">
			<form method=\"post\" action=''>
					<table border=\"0\" width=\"155\">
						<tr>
							<td>
								Username:
								<input type=\"text\" name=\"username\" maxlength=\"12\" />
							</td>
						</tr>
						<tr>
							<td>
								Password:
								<input type=\"password\" name=\"password\" maxlength=\"12\" />
							</td>
						</tr>
						<tr>
							<td align='center'>
								<input type=\"submit\" style=\"width:48%;\" name=\"login\" value=\"Login\" />
								".$return."
							</td>
						</tr>
					</table>
			</form>
		</div>";
}
?>
 
Custom Title Activated
Member
Joined
Dec 21, 2007
Messages
1,040
Reaction score
127
Alright, the mysql_query and query_row I understand how to use. For session variables, I will post my login script.

PHP:
<?php 
if(isset($_SESSION['id'])){
    echo "<meta http-equiv=refresh content=\"0\" />";
}else{
    if(isset($_POST['login'])) {
        $u = mysql_real_escape_string(stripslashes($_POST['username']));
        $p = mysql_real_escape_string(stripslashes($_POST['password']));
        $s = mysql_query("SELECT * FROM `accounts` WHERE `name`='".$u."'") or die(mysql_error());
        $i = mysql_fetch_array($s);
        
        if($i['password'] == hash('sha512',$p.$i['salt']) || sha1($p) == $i['password']){
            $userz = mysql_query("SELECT * FROM `accounts` WHERE `name`='".$i['name']."' AND `password`='".$i['password']."'") or die(mysql_error());
            $auser = mysql_fetch_array($userz);
            $_SESSION['id'] = $auser['id'];
            $_SESSION['name'] = $auser['name'];
            $gmammt = $gmlevel - 1;
            if($auser['gm'] > $gmammt){
                $_SESSION['gm'] = $auser['gm'];
            }
        } else {
            $return = "Invalid username or password.";
        }
    }
        echo "
        <div class=\"regtext\">
            <form method=\"post\" action=''>
                    <table border=\"0\" width=\"155\">
                        <tr>
                            <td>
                                Username:
                                <input type=\"text\" name=\"username\" maxlength=\"12\" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Password:
                                <input type=\"password\" name=\"password\" maxlength=\"12\" />
                            </td>
                        </tr>
                        <tr>
                            <td align='center'>
                                <input type=\"submit\" style=\"width:48%;\" name=\"login\" value=\"Login\" />
                                ".$return."
                            </td>
                        </tr>
                    </table>
            </form>
        </div>";
}
?>

Number one rule: Do not use meta refresh. Use "header(location: filename.php);".

I'm not quite sure at how others work around with their login script but I, instead of what you're doing (getting the information out of the database, match it with the information posted and then parse the rest of the script), match the posted information directly with the database, using mysql_num_rows.

Something like this:

PHP:
function userCheck($username, $password) {

    return $query = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'"));

}

If 1, success. If 0, denial. Simple, effective..
I believe far more effective than your way of doing things. You see, your first query is completely unrequired. Why check only for the username? If, upon registration, you already check if there's already a username with that name and if there is you won't allow them to register, why not automatically check, on login, if there's a match between a username/password combination? You're wasting perfomance.

Also, when you're fetching your user data, on the SELECT query you need to specify what you want to retrieve from the table. I see you were trying to fetch your username's level, but you didn't select it. I'm sure there's a better way of doing what you want, perfomance wise, but start by trying to fetch the username+password+gmlevel first and then working around it.

Also, instead of "$auser", why not "$aUser" ? It's pretty tough to read what you wrote up there :)

Good luck mate!




(Anyone feel free to correct me please.. If I said anything wrong, I'd like to know it.)
 
Joined
Dec 15, 2009
Messages
1,387
Reaction score
236
Number one rule: Do not use meta refresh. Use "header(location: filename.php);".

I'm not quite sure at how others work around with their login script but I, instead of what you're doing (getting the information out of the database, match it with the information posted and then parse the rest of the script), match the posted information directly with the database, using mysql_num_rows.

Something like this:

PHP:
function userCheck($username, $password) {

    return $query = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'"));

}

If 1, success. If 0, denial. Simple, effective..
I believe far more effective than your way of doing things. You see, your first query is completely unrequired. Why check only for the username? If, upon registration, you already check if there's already a username with that name and if there is you won't allow them to register, why not automatically check, on login, if there's a match between a username/password combination? You're wasting perfomance.

Also, when you're fetching your user data, on the SELECT query you need to specify what you want to retrieve from the table. I see you were trying to fetch your username's level, but you didn't select it. I'm sure there's a better way of doing what you want, perfomance wise, but start by trying to fetch the username+password+gmlevel first and then working around it.

Also, instead of "$auser", why not "$aUser" ? It's pretty tough to read what you wrote up there :)

Good luck mate!




(Anyone feel free to correct me please.. If I said anything wrong, I'd like to know it.)
why can't we use meta refresh? isn't that the most basic stuff and easy?
 
Custom Title Activated
Member
Joined
Dec 21, 2007
Messages
1,040
Reaction score
127
why can't we use meta refresh? isn't that the most basic stuff and easy?
I'll quote what timebomb posted in another thread -

timebomb said:
3) I explained this recently in another thread, but that thread got deleted for some reason unbeknownst to me... you should not be using meta refresh to refresh pages. You should use header('Location: '). The only reason you wouldn't be able to use this is if you already sent out content before sending out headers. In that case, it shows bad application design. If you have no other choice, meta refresh does usually work, although it is incorrect HTML if it is anywhere outside of the head tag.
 
Google my name...
Joined
Nov 9, 2011
Messages
483
Reaction score
151
PHP:
$ucoins = mysql_query("SELECT votingpoints FROM accounts WHERE `name`='".$_SESSION['name']."' LIMIT 1");
// Best thing to do is limit it to 1 when using fetch row. The session is set in your login.
$data = mysql_fetch_row($ucoins);
//This pulls out any requested data from a single row in the database.
echo "You have " + $data[0] " U-Coin(s)";
// Since you set votingpoints in the query [0] is the votingpoints data.

Okay what I've posted above should work, unless the session doesn't work, in which case add session_start(); in the head of the html.

If you want more data to be pulled out turn "votingpoints" into "votingpoints, username, password" etc.. and $data[0] = votingpoints, $data[1] = username, $data[2] = password.

Very simple, if you need help with anything else drop me a private message.

I would also like to teach you to not use mysql_ syntax as it is outdated, look into PDO and send me a private message if you want to implement PDO, I can help you out with that since it's a different syntax and if you do it wrong you'll tear your hair out.

All the best,
Richard Komakech.
 
Back
Top