Capturing Values From Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Capturing Values From Database

Post by Smudly »

MySQL Version: 5.0.91

Hi, I am currently working on a Leveling system for users for my website. I'm creating an exp.php file that will contain how much exp is required for the next level up.

I am currently attempting to capture the user's Level which is stored inside the database. However, it isn't being set to my variable. I try echoing it out to see the output, but it comes up blank. Both Exp and Level come up blank. I'm obviously doing the query wrong, otherwise it would be showing.

The purpose of this file is to check what level the current user is (using sessions). Once it gets its level, there will be multiple if statements to determine how much more experience is required to level up. Once the experience is gained, the new level of the user will be increased by one.

A good part of this script is pseudocode (I'm currently not testing those sections).

Code: Select all

<?php
session_start();
// This include redirects users to home page if they are not an admin
include('admin.php');
// Connect to database
include('inc/connect.php');

$userid = $_SESSION['userid'];

// Temporary Pseudocode, Ignore this for now.
if(level==1){
$exptolvl = 100;
	if($usersexp==$exptolevel){
	$level = 2;	
	}
}
// Temporary Pseudocode, Ignore this for now.
if (surfed){
$usersexp += 100;
}

// Query begins.
$expquery = mysql_query("SELECT * FROM users WHERE exp='$exp' AND level='$newlevel'");
$row = mysql_num_rows($expquery);

while($row = mysql_fetch_assoc($expquery))
{
    $exp = $row['exp'];
    $level = $row['level'];
}

echo $exp;
echo $level;


$inputexp = mysql_query("UPDATE users SET exp='$exp' and level='$newlevel' WHERE id='$userid'");

?>
And my table called "users" looks like:

Code: Select all

id  	int(11)   	No   	    	 
username 	varchar(25) 	No  	  	 
level 	int(2) 	No  	1  	 
email 	varchar(64) 	No  	  	 
fname 	varchar(25) 	No  	  	 
lname 	varchar(25) 	No  	  	 
member 	tinyint(1) 	No  	  	 
referrer 	varchar(25) 	No  	  	 
joindate 	date 	No  	  	 
lastsurfed 	date 	No  	  	 
credits 	decimal(9,3) 	No  	  	 
exp 	int(6) 	No  	  	 
password 	varchar(32) 	No  	  	 
ip 	varchar(15) 	No  	  	 
I get no errors. It just isn't capturing the values correctly.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Capturing Values From Database

Post by andyhoneycutt »

Your code isn't very readable due to all the html entities... That said, it looks like your update statement is wrong. Use a comma instead of an "and". Value1 = 1, Value2 = 2, etc. Not Value1 = 1 and Value2 = 2.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Capturing Values From Database

Post by andyhoneycutt »

I'd be happy to look at your code if you clean it up so it's readable, to actually help you solve the problem you mentioned.
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Re: Capturing Values From Database

Post by Smudly »

Sorry about that, I was using the wrong

Code: Select all

 format.

Here is my updated code, with a new problem. The level and exp now echo out, but upon refreshing the page, the values are reset to 0. The original values shown on the page and in the database were: Level = 1, Exp = 100.

I'm not sure why it is resetting the values. I'm not getting any errors.

[syntax=php]<?php
session_start();
include('admin.php');
include('inc/connect.php');

$userid = $_SESSION['userid'];

// Temporary Pseudocode
if($level==1){
	$exptolvl = 100;
	if($exp==$exptolevel){
	$newlevel = 2;	
	}
}

// Temporary Pseudocode
// if (surfed){
// $newexp += 100;
// }

$expquery = mysql_query("SELECT * FROM users WHERE id='$userid'");
$row = mysql_num_rows($expquery);

while($row = mysql_fetch_assoc($expquery))
{
    $exp = $row['exp'];
	$level = $row['level'];
}

echo $exp."<br />";
echo $level;


$inputexp = mysql_query("UPDATE users SET exp='$newexp', level='$newlevel' WHERE id='$userid'");

?>[/syntax]
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Capturing Values From Database

Post by andyhoneycutt »

Your first if block doesn't look like it can fire: you're never setting $exp to any value, you just compare it to $exptolevel, so it'll only fire when $exptolevel is zero, which is never. Then you never define $newexp, but use it in your update statement. And as a result of your first if statement block not properly firing, $newlevel is always zero, as well. That's why you're getting zeroes.
Post Reply