So this is a tutorial about how you can make your script easier..
You can do this by using functions, so you don't have to type the whole code again and again and again =p
You can have functions that return something, or just functions that do something..
So here's an example for a function to get player data..
Code: Select all
function getPlayerData($userid, $row) {
$query = mysql_query("SELECT * FROM users WHERE id = '".$userid."'");
$row2 = mysql_fetch_assoc($query);
return $row2[$row];
}
So what i do is make a page functions.php and include it in my config.php
then you can use your function like that
Code: Select all
echo "Your name is ".getPlayerData($_SESSION['id'], 'username');
Ok, so here's an example how to set player data
Code: Select all
function setPlayerData($userid, $row, $value) {
mysql_query("UPDATE users SET ".$row." = '".$value."' WHERE id = '".$userid."'");
}
I also include this in my functions.php page
Now i'm going to give you an example of using the 2 functions together
Code: Select all
$currentmoney = getPlayerData($_SESSION['id'], 'money');
$newmoney = $currentmoney - 10;
setPlayerData($_SESSION['id'], 'money', $newmoney);
I just typed these here, so there can be some faults, but it's just to explain it all,
I did my best to explain it so =p