Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Capturing Values From Database
Old 06-16-2010, 11:10 AM Capturing Values From Database
Skilled Talker

Posts: 83
Trades: 0
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).

PHP Code:
<?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:
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.
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-16-2010, 11:18 AM Re: Capturing Values From Database
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
I am not sure about your error from briefly looking at the code, however I word of advice,

if you have a table with multiple cells of info you are not going to interact with then limit the cells you are selecting in your statements:

PHP Code:
$expquery mysql_query("SELECT * FROM users WHERE exp='$exp' AND level='$newlevel'");

//becomes

$expquery mysql_query("SELECT `exp`,`level` FROM users WHERE exp='$exp' AND level='$newlevel'"); 
This is from the top of my head so im not 100% about the `'s ive used. But its along the right lines.
__________________

Please login or register to view this content. Registration is FREE

Learn professional skills from professional people, from $6.50 a month.
Phunk Rabbit is offline
Reply With Quote
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 06-16-2010, 11:21 AM Re: Capturing Values From Database
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
try using the correct variable in your query. $exp has no value in your SELECT query.

AND avoid using SELECT * in queries it is inefficient and leads to wasted resources on the server.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-16-2010, 12:25 PM Re: Capturing Values From Database
Skilled Talker

Posts: 83
Trades: 0
Alright, thank you for the tips. I tried using:

PHP Code:
"SELECT `id` FROM users WHERE id='$userid'" 
but it keeps it from working.

It works if I leave it as:

PHP Code:
"SELECT * FROM users WHERE id='$userid'" 
Not sure why.

Here is my updated code. It echoes out Level and Exp now, however after refreshing the page, the values are reset to 0. The original values in the database and shown on the page are: Level = 1, Exp = 100.

Any idea what could cause it to reset?
I'm not getting any errors.

Here is my updated code.
PHP Code:
<?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'");

?>
Smudly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Capturing Values From Database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13527 seconds with 12 queries