Akenatehm Posted February 7, 2009 Share Posted February 7, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/ Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 you are using $row instead of $results Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756512 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 Omg wow...such a stupid mistake.. Thanks for pointing out to me. Much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756517 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 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"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756518 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 okay well are you using the right column name? You are using 'points' as the column name and in your query you are using 'points' as the table name. Is that really how you have your table setup? Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756520 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 Yes. The table name is points and the field name is points. Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756522 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 Any Idea anyone? Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756532 Share on other sites More sharing options...
sasa Posted February 7, 2009 Share Posted February 7, 2009 change lipn while ($results = mysql_fetch_array($findpoints, MYSQL_NUM)){ to while ($results = mysql_fetch_array($findpoints, MYSQL_ASSOC)){ Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756555 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 OMG. Thank you so much! It works like a charm. Outputs: You have200points Anyway I can put a space in there before and after 200? Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756564 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 Anyway I can put a space in there before and after 200? no. Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756574 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756581 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756586 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 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"; } } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756589 Share on other sites More sharing options...
fLaVV Posted February 7, 2009 Share Posted February 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756591 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756593 Share on other sites More sharing options...
fLaVV Posted February 7, 2009 Share Posted February 7, 2009 $deduct = mysql_query("UPDATE characters SET gold = gold - " . $amount . " WHERE name = '" . $character . "' AND gold >= '" . $amount . "';"); At the end of the code, it looks like your double closing the query ;"); Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756595 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 $deduct = mysql_query("UPDATE characters SET gold = gold - " . $amount . " WHERE name = '" . $character . "' AND gold >= '" . $amount . "';"); At the end of the code, it looks like your double closing the query ;"); Still didn't work.. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756596 Share on other sites More sharing options...
sasa Posted February 7, 2009 Share Posted February 7, 2009 variable $amount is empty try to echo it before query Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756600 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756603 Share on other sites More sharing options...
Akenatehm Posted February 7, 2009 Author Share Posted February 7, 2009 Hmmmm. I just realised that I was echoing nothing on the last line lol. Fixed the variable and now everything works.!! Thanks guys for all your help! It's been great! See you guys in the near future (hopefully not). Lol. Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/144166-solved-getting-certain-field-from-a-row-problems/#findComment-756608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.