So im following the tutorials on http://buildingbrowsergames.com/ and I'm having some issues. I have two functions and they do exactly what I want. However if I execute both of them I get some errors. Here is the code for the two functions:
<?php
function getStat($statName,$userID)
{
require_once 'config.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("SELECT user_stat.user_statValue
FROM user_stat
INNER JOIN stats ON (user_stat.statID = stats.statID)
WHERE stats.statShortName = '%s' AND user_stat.userID = '%s'",
mysql_real_escape_string($statName),
mysql_real_escape_string($userID));
$result = mysql_query($query);
list($value) = mysql_fetch_row($result);
return $value;
}
function setStat($statName,$userID,$statValue)
{
require_once 'config.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = sprintf("UPDATE user_stat SET user_statValue = user_statValue + '%s' WHERE statID = (SELECT statID FROM stats WHERE statFullName = '%s' OR statShortName = '%s') AND userID = '%s'",
mysql_real_escape_string($statValue),
mysql_real_escape_string($statName),
mysql_real_escape_string($statName),
mysql_real_escape_string($userID));
$result = mysql_query($query);
}
?>
Now When I do the staments below in this order I get this error:
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\proto\stats.php on line 14
If I switch them around the getStat() works however the setStat() doesnt do anything.
setStat("ASRIF",$_SESSION['userID'],10);
echo "User '" .$_SESSION['username'] ."' your stat is " .getStat("ASRIF",$_SESSION['userID']);