...

View Full Version : Getting error when everything looks right.



Smudly
06-17-2010, 12:56 AM
For some reason, the following code is giving this error:
Parse error: syntax error, unexpected '}' in /home/content/s/m/u/smudlys/html/ratestest.php on line 21

This is the line after the first if statement.


MySql version: 5.0.19

Here is my code:

<?php
/// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];
$getmember = mysql_query("SELECT * FROM users WHERE username='$user'");
while ($row = mysql_fetch_assoc($getmember))
{
$member = $row['member'];
}
$getlevel = mysql_query("SELECT * FROM userstats WHERE username='$user'");
while ($row = mysql_fetch_assoc($getlevel))
{
$level = $row['level'];
}

$addrate = .005;

if($member==0){
$rate = ($level*$addrate+.50)
}
if($member==1){
$rate = ($level*$addrate+1)
}

?>

Jazz914
06-17-2010, 01:17 AM
<?php
/// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];
$getmember = mysql_query("SELECT * FROM users WHERE username='$user'");
while ($row = mysql_fetch_assoc($getmember))
{
$member = $row['member'];
}
$getlevel = mysql_query("SELECT * FROM userstats WHERE username='$user'");
while ($row = mysql_fetch_assoc($getlevel))
{
$level = $row['level'];
}

$addrate = .005;

if($member==0){
$rate = ($level*$addrate+.50);
}
if($member==1){
$rate = ($level*$addrate+1);
}

?>

Silly mistake ^_^

Smudly
06-17-2010, 01:18 AM
Silly me ;)

Thanks alot.

Just one more thing if you don't mind.

When I try echoing out $rate, it says it is .5

When in reality, it should be .505
(user's level is 1, and member=0)

How do I make it include all the decimal places?

Thanks!

Jazz914
06-17-2010, 01:35 AM
Have a look at number_format() (http://php.net/manual/en/function.number-format.php) May do what you want :)

_Aerospace_Eng_
06-17-2010, 01:41 AM
What you are getting as values for $level? I think the scope might local to just the while loops. In this case though the while loops aren't necessary.

<?php
/// Include Admin Page Here ///
include('inc/connect.php');
$user = $_SESSION['username'];
$getmember = mysql_query("SELECT * FROM users WHERE username='$user'");
$row = mysql_fetch_assoc($getmember);
$member = $row['member'];
$getlevel = mysql_query("SELECT * FROM userstats WHERE username='$user'");
$row2 = mysql_fetch_assoc($getlevel);
$level = $row2['level'];

$addrate = .005;

if($member==0){
$rate = ($level*$addrate+.50);
}
if($member==1){
$rate = ($level*$addrate+1);
}

Try that.

Smudly
06-17-2010, 01:44 AM
Great thanks!

I looked over some of the stuff there, and have tried various methods but to no avail.

Currently this is what I'm working with.


$addrate = 0.005;
$free = 0.50;
$paying = 1;


// Free Member
if($member==0){
$rate = ($level*$addrate+$free);
$rate = number_format($rate, 3, '.', '');
echo $rate;
}

Once it is echoed out, it shows a result of 0.500

For some reason it isn't 0.505 as expected.

Anyone have another method of fixing this?

Jazz914
06-17-2010, 01:47 AM
When you get 0.500 what value is $level? Try doing this:


<?php echo 'Rate:'. $rate.'<br> Level:'. $level; ?>

where you currently have

<?php echo $rate; ?>

Smudly
06-17-2010, 01:49 AM
Typing that in gives the following result:

Rate:0.500
. Level:

EDIT:

I saw that you re-edited your post, so I double checked and placed it where you wanted me to.

here is the result:

Rate:0.500
. Level:

Here is what my table looks like:

id int(11) No
username varchar(25) No
level int(2) No
exp int(6) No
credits decimal(9,3) No
todaysurfed int(4) No
totalsurfed int(11) No
timesurfed time No
lastsurfed date No

Jazz914
06-17-2010, 01:57 AM
This shows level isn't being defined properly, or has no value being assigned to it

Check what value the user has in the mysql table.

Smudly
06-17-2010, 01:59 AM
The value for level for the current user is "1".

Jazz914
06-17-2010, 02:11 AM
OOOH Lol, My mistake sorry.
I just noticed you are whiling through a mysql fetch which isn't needed. Try doing this instead:


<?php
/// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];
$sql = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'");
$row = mysql_fetch_assoc($sql);

$member = $row['member'];
$level = $row['level'];

$addrate = .005;

if ($member == 0) {
$rate = ($level*$addrate+.50);
}

if ($member==1) {
$rate = ($level*$addrate+1);
}

echo 'Rate:'. $rate.'<br> Level:'. $level;
?>

That should work.

_Aerospace_Eng_
06-17-2010, 02:12 AM
Level is still not getting assigned a value which could mean that your query is failing. Post your current code please.

Smudly
06-17-2010, 02:16 AM
I'm still getting the same output as before:
Rate:0.500
. Level:

Here is my updated code.


<?php

// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];

$getmember = mysql_query("SELECT * FROM users WHERE username='$user'");
$row = mysql_fetch_assoc($getmember);
$member = $row['member'];
$getlevel = mysql_query("SELECT * FROM userstats WHERE username='$user'");
$row2 = mysql_fetch_assoc($getlevel);
$level = $row2['level'];

$addrate = 0.005;
$free = 0.50;
$paying = 1;

// Free Member
if($member==0){
$rate = ($level*$addrate+$free);
$rate = number_format($rate, 3, '.', '');
echo 'Rate:'. $rate.'<br> Level:'. $level.'<br />';
}
// Paying Member
if($member==1){
$rate = ($level*$addrate+$paying);
echo $rate;
}

?>

Thanks

Jazz914
06-17-2010, 02:18 AM
Can you try the code I posted? :) Here ^_^ (http://codingforums.com/showpost.php?p=965159&postcount=11)

If this doesn't work I have another theory :)

Smudly
06-17-2010, 02:21 AM
Ahh sorry ;)

K i tried it as is, but same thing as before.

Also, something I failed to mention earlier.

I am using two separate tables for this script.

The first called 'users':

id int(11) No
username varchar(25) No
email varchar(64) No
fname varchar(25) No
lname varchar(25) No
member tinyint(1) No
referrer varchar(25) No
joindate date No
password varchar(32) No
ip varchar(15) No

The second called 'userstats':

id int(11) No
username varchar(25) No
level tinyint(2) No
exp int(6) No
credits decimal(9,3) No
todaysurfed int(4) No
totalsurfed int(11) No
timesurfed time No
lastsurfed date No

Thanks again

EDIT: I tried editting the following code:

$sql = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'");
$row = mysql_fetch_assoc($sql);

$member = $row['member'];
$level = $row['level'];

Changed it to this:

$sql = mysql_query("SELECT * FROM `userstats` WHERE `username`='".$user."'");
$row = mysql_fetch_assoc($sql);
$level = $row['level'];

$sql1 = mysql_query("SELECT * FROM `users` WHERE `username`='".$user."'");
$row1 = mysql_fetch_assoc($sql1);
$member = $row['member'];

Only difference is it echoed out like:
Rate:0.5
Level:

Jazz914
06-17-2010, 02:27 AM
Oops xD try this one instead then:

<?php
/// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];
$sql = mysql_query("SELECT * FROM `users` WHERE `username` ='".$user."'");
$row = mysql_fetch_assoc($sql);

$sql2 = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'");
$row2 = mysql_fetch_assoc($sql2);


$member = $row['member'];
$level = $row2['level'];

$addrate = .005;

if ($member == 0) {
$rate = ($level*$addrate+.50);
}

if ($member == 1) {
$rate = ($level*$addrate+1);
}

echo 'Rate:'. $rate.'<br> Level:'. $level;
?>

Smudly
06-17-2010, 02:32 AM
Copied and pasted it in exactly as is, getting the following output:

Rate:0.5
Level:

I'm unsure why it isn't working. The logic all seems to be right.

Jazz914
06-17-2010, 02:34 AM
edit:

<?php
$sql2 = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'");
$row2 = mysql_fetch_assoc($sql2);
?>

with:

<?php
$sql2 = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'") or die(mysql_error());
$row2 = mysql_fetch_assoc($sql2);
?>

Smudly
06-17-2010, 02:38 AM
Alright, I edited that section.

No errors were returned. Same output as before.

Jazz914
06-17-2010, 02:45 AM
can you do the following:

Do the following (In phpmyadmin):
Click on the table `userstats` then go to "Export" located on the top left:
Export type should be: SQL
Uncheck "Save as file"

Then paste that data in here

Smudly
06-17-2010, 02:47 AM
K it's sent to you.

_Aerospace_Eng_
06-17-2010, 02:48 AM
Try this

<?php

// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];

$getmember = mysql_query("SELECT * FROM users WHERE username='$user'") or die(mysql_error());
$row = mysql_fetch_assoc($getmember);
$member = $row['member'];
$getlevel = mysql_query("SELECT * FROM userstats WHERE username='$user'") or die(mysql_error());
$row2 = mysql_fetch_assoc($getlevel);
$level = $row2['level'];

$addrate = 0.005;
$free = 0.50;
$paying = 1;

// Free Member
if($member==0){
$rate = ($level*$addrate+$free);
$rate = number_format($rate, 3, '.', '');
echo 'Rate:'. $rate.'<br> Level:'. $level.'<br />';
}
// Paying Member
if($member==1){
$rate = ($level*$addrate+$paying);
echo $rate;
}

?>
Post your results if any.

Smudly
06-17-2010, 02:50 AM
Alright, tried that code, and getting the following output:


Rate:0.500
Level:

There are no errors.

Jazz914
06-17-2010, 02:54 AM
Can you do the same for the users table, removing any passwords

Jazz914
06-17-2010, 02:57 AM
This is kind of silly... No-one questioned the session LOL

can you echo do the following:


<?php echo($_SESSION['username']); ?>

I'm tired xD

Smudly
06-17-2010, 02:59 AM
Alright, tried echoing that line, and nothing.
No errors, no output.

I appreciate your help. I would be getting no where without you guys ;)

Jazz914
06-17-2010, 03:02 AM
hahahahahha There's the problem, you have no session set, therefore nowhere to grab the users level :) The reason why the coniditional statement "$member == 0" was being activated was because $level was being assigned a null value which is equivalent to 0 :P

Reassign the session by relogging in or something and the script should work.

Smudly
06-17-2010, 03:07 AM
haha oh wow!

It now outputs the username correctly.
However, it still doesn't output the level.

Here's my current code:
(Don't feel obligated to help, you've done plenty!)

<?php
session_start();
/// Include Admin Page Here ///
include('inc/connect.php');

$user = $_SESSION['username'];
$sql = mysql_query("SELECT * FROM `users` WHERE `username` ='".$user."'");
$row = mysql_fetch_assoc($sql);

$sql2 = mysql_query("SELECT * FROM `userstats` WHERE `username` ='".$user."'") or die(mysql_error());
$row2 = mysql_fetch_row($sql2);


$member = $row['member'];
$level = $row2['level'];


$addrate = .005;

if ($member == 0) {
$rate = ($level*$addrate+.50);
$rate = number_format($rate, 3, '.', '');
echo $rate;
}

if ($member == 1) {
$rate = ($level*$addrate+1);
}

echo 'Rate:'. $rate.'<br> Level:'. $level;
?>


The output is:
Rate:0.500
Level:

Side note, the rate should be 0.505
But I think that will fix once I get the $level variable working.

EDIT:
Inside my database, the value for level has been set to 0 for my current user. Not sure if that will help us figure out what is wrong with it.

Jazz914
06-17-2010, 03:10 AM
You never updated the code xD

Here ^_^ (http://www.codingforums.com/showpost.php?p=965169&postcount=18)

Smudly
06-17-2010, 03:11 AM
FIXED!!!!


I owe you. I'll send you a pizza or something haha

Jazz914
06-17-2010, 03:17 AM
haha, Thats good ^_^ If only I wasn't tired, I would of gone asleep but your problem confused me so much I had to figure it out xD

Tiredness can kill, and apparently it can hide the words "$user = $_SESSION['username'];" for god knows how many posts!

Glad you got it working! :D Time for bed xD



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum