Jump to content

[SOLVED] Getting Certain Field From a Row Problems


Akenatehm

Recommended Posts

Hey Guys,

 

I am trying to find out how many points someone has and displaying it on the screen.

 

The code is here:

<?php

$host = "localhost";
$name = "username";
$pass = "pass";
$db = "db";

$con = mysql_connect($host,$name,$pass);
if (!$con)
{
die('Failed to Connect:' . mysql_error());
}

$selectdb = mysql_select_db($db);
if (!$selectdb)
{
die('Failed to Select Database:' . mysql_error());
}

$username = $_COOKIE['Username'];
$checkusername = mysql_query("SELECT * FROM points WHERE account = '$username' ");

if(!$checkusername)
{
die('Failed to Check Your Username' . mysql_error());
}
elseif(mysql_num_rows($checkusername) == 0)
{
echo "Your Username Does not Exist on the Database";
}
else 
{
$findpoints = mysql_query("SELECT * FROM points WHERE account = '$username'");

if(!$findpoints)
{
die('Failed to Find Your Points' . mysql_error());
}
else
{
while ($results = mysql_fetch_array($findpoints, MYSQL_NUM)){
echo "You have" . $row['points'] . "points";
}
}
}

?>

 

The output is just:

You havepoints

 

But I need it to say You have 50 points. Where 50 is the number from the DB.

 

Help would be Great!

Link to comment
Share on other sites

I fixed that. AND it still shows the same thing

 

<?php

$host = "localhost";
$name = "user";
$pass = "pass";
$db = "db";

$con = mysql_connect($host,$name,$pass);
if (!$con)
{
die('Failed to Connect:' . mysql_error());
}

$selectdb = mysql_select_db($db);
if (!$selectdb)
{
die('Failed to Select Database:' . mysql_error());
}

$username = $_COOKIE['Username'];
$checkusername = mysql_query("SELECT * FROM points WHERE account = '$username' ");

if(!$checkusername)
{
die('Failed to Check Your Username' . mysql_error());
}
elseif(mysql_num_rows($checkusername) == 0)
{
echo "Your Username Does not Exist on the Database";
}
else 
{
$findpoints = mysql_query("SELECT points FROM points WHERE account = '$username'");

if(!$findpoints)
{
die('Failed to Find Your Points' . mysql_error());
}
else
{
while ($results = mysql_fetch_array($findpoints, MYSQL_NUM)){
echo "You have" . $results['points'] . "points";
}
}
}

?>

Link to comment
Share on other sites

Really.. Well ok..

 

Uhm I am getting a problem in using the same script on another document.

 


<?php

$host = "localhost";
$name = "username";
$pass = "pass";
$db = "db";

$con = mysql_connect($host,$name,$pass);
if (!$con)
{
die('Failed to Connect:' . mysql_error());
}

$selectdb = mysql_select_db($db);
if (!$selectdb)
{
die('Failed to Select Database:' . mysql_error());
}

$username = $_COOKIE['Username'];
$item = $_POST['item'];
$checkusername = mysql_query("SELECT * FROM points WHERE account = '$username' ");

if(!$checkusername)
{
die('Failed to Check Your Username' . mysql_error());
}
elseif(mysql_num_rows($checkusername) == 0)
{
echo "Your Username Does not Exist on the Database";
}
else 
{
$enoughpoints = mysql_query("SELECT requiredpoints FROM rewards WHERE name = '$item'");

if(!$enoughpoints)
{
die('Failed to Check Points of Item' . mysql_error());
}
else
{
while ($results = mysql_fetch_array($enoughpoints, MYSQL_ASSOC))
{	
$cost = $results['points'];
}
$userpoints = mysql_query("SELECT * FROM points WHERE account = '$username'");
$userpointsresults = mysql_fetch_array($userpoints, MYSQL_ASSOC);
$userhaspoints = $userpointsresults['points'];

if(!$userpoints)
{
die('Failed to Check Your Points' . mysql_error());
}
else
{
if($cost > $userhaspoints)
{
echo "You do not have enough points";
}
else{
$character = $_POST['charactername'];
$amount = $cost;
$deduct = mysql_query("UPDATE characters SET gold = gold - " . $amount . " WHERE name = '" . $character . "' AND gold >= '" . $amount . "';");
if (!$deduct)
{
die('Failed to Deduct Gold' . mysql_error());
}
else{
$getguid = mysql_query("SELECT guid FROM characters WHERE name = '$username'");
if(!$getguid)
{
die('Could Not Find GUID of User' . mysql_error());
}
else{
$sortguid = mysql_fetch_array($getguid, MYSQL_ASSOC);
$userhaspoints = $sortguid['guid'];

echo "$guid";
}
}
}
}
}
}

?>

 

That is outputting this error:

Failed to Deduct GoldYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE name = 'Cody' AND gold >= ''' at line 1

 

Where Cody is my username.

Link to comment
Share on other sites

Ok. Turns out I hadn't fixed the setting properly when transferring it from another script.

 

I am getting this weird error though:

 

Your Username Does not Exist on the Database

 

From this area of code:

$username = $_COOKIE['Username'];
$item = $_POST['item'];
$checkusername = mysql_query("SELECT * FROM points WHERE account = '$username' ");

if(!$checkusername)
{
die('Failed to Check Your Username' . mysql_error());
}
elseif(mysql_num_rows($checkusername) == 0)
{
echo "Your Username Does not Exist on the Database";
}

Link to comment
Share on other sites

Ok. Turns out I hadn't fixed the setting properly when transferring it from another script.

 

I am getting this weird error though:

 

Your Username Does not Exist on the Database

 

From this area of code:

$username = $_COOKIE['Username'];
$item = $_POST['item'];
$checkusername = mysql_query("SELECT * FROM points WHERE account = '$username' ");

if(!$checkusername)
{
die('Failed to Check Your Username' . mysql_error());
}
elseif(mysql_num_rows($checkusername) == 0)
{
echo "Your Username Does not Exist on the Database";
}

 

Wow..This is turning into a mess.

 

The cookie expired and that is why i was getting that error. The error still is the same.

 

Failed to Deduct GoldYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE account = 'Cody' AND points >= ''' at line 1

 

From

<?php

$host = "localhost";
$name = "username";
$pass = "pass";
$db = "db";

$con = mysql_connect($host,$name,$pass);
if (!$con)
{
die('Failed to Connect:' . mysql_error());
}

$selectdb = mysql_select_db($db);
if (!$selectdb)
{
die('Failed to Select Database:' . mysql_error());
}

$username = $_COOKIE['Username'];
$item = $_POST['item'];
$checkusername = mysql_query("SELECT * FROM points WHERE account = '$username' ");

if(!$checkusername)
{
die('Failed to Check Your Username' . mysql_error());
}
elseif(mysql_num_rows($checkusername) == 0)
{
echo "Your Username Does not Exist on the Database";
}
else 
{
$enoughpoints = mysql_query("SELECT requiredpoints FROM rewards WHERE name = '$item'");

if(!$enoughpoints)
{
die('Failed to Check Points of Item' . mysql_error());
}
else
{
while ($results = mysql_fetch_array($enoughpoints, MYSQL_ASSOC))
{	
$cost = $results['points'];
}
$userpoints = mysql_query("SELECT * FROM points WHERE account = '$username'");
$userpointsresults = mysql_fetch_array($userpoints, MYSQL_ASSOC);
$userhaspoints = $userpointsresults['points'];

if(!$userpoints)
{
die('Failed to Check Your Points' . mysql_error());
}
else
{
if($cost > $userhaspoints)
{
echo "You do not have enough points";
}
else{
$character = $_POST['charactername'];
$amount = $cost;
$deduct = mysql_query("UPDATE points SET points = points - " . $amount . " WHERE account = '" . $username . "' AND points >= '" . $amount . "';");
if (!$deduct)
{
die('Failed to Deduct Gold' . mysql_error());
}
else{
$getguid = mysql_query("SELECT guid FROM characters WHERE name = '$username'");
if(!$getguid)
{
die('Could Not Find GUID of User' . mysql_error());
}
else{
$sortguid = mysql_fetch_array($getguid, MYSQL_ASSOC);
$userhaspoints = $sortguid['guid'];

echo "$guid";
}
}
}
}
}
}

?>

Link to comment
Share on other sites

well if you read the error, it says:

 

You have an error in your SQL syntax.

 

This means

 

$deduct = mysql_query("UPDATE characters SET gold = gold - " . $amount . " WHERE name = '" . $character . "' AND gold >= '" . $amount . "';");

 

is throwing an error because it isnt executing properly.

 

Just taking a quick stab, i would guess you need to change

 

UPDATE characters SET gold = gold - " . $amount . "

 

to

 

UPDATE characters SET gold = 'gold - " . $amount . "'

 

I think the "gold - " is messing up the query, maybe by adding the (') it MIGHT work.

 

I am not 100% sure, I am just trying to help ya out, may want some second advice, but thats what i would try first.

 

 

Link to comment
Share on other sites

well if you read the error, it says:

 

You have an error in your SQL syntax.

 

This means

 

$deduct = mysql_query("UPDATE characters SET gold = gold - " . $amount . " WHERE name = '" . $character . "' AND gold >= '" . $amount . "';");

 

is throwing an error because it isnt executing properly.

 

Just taking a quick stab, i would guess you need to change

 

UPDATE characters SET gold = gold - " . $amount . "

 

to

 

UPDATE characters SET gold = 'gold - " . $amount . "'

 

I think the "gold - " is messing up the query, maybe by adding the (') it MIGHT work.

 

I am not 100% sure, I am just trying to help ya out, may want some second advice, but thats what i would try first.

 

Unfortunately, that didn't work. Sorry.

Link to comment
Share on other sites

variable $amount is empty

try to echo it before query

 

Ok, It wasn't echoing. So I searched.

 

and realised that $cost = ['points']

 

when it needed to be $cost = ['requiredpoints']

 

So i fixed it. Now it echos it.

 

I removed teh echo.

 

But the final part of the script about getting the guid is not doing anything just showing up blank on the screen.. Help with that wud be great.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.