I'm trying to get some data from a database using a PHP function, but for some reason, when I do this several times, you get a MySQL connection error.
$heat=getStat("heat", $userid); $cash=getStat("cash", $userid); echo mysql_error();
I use the code above to assign variables by calling a function that retrieves statistics from the database.
When I use the above codes separately, they work. But when I fold them, they fail.
Is this a simple mistake for you-beginner-noob-programming?
I forgot to publish the function, here it is:
function getStat($statName,$userID) { require_once 'config.php'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die('Error connecting to MySQL' . mysql_error()); mysql_select_db($dbname); $query = sprintf("SELECT value FROM user_stats WHERE stat_id = (SELECT id FROM stats WHERE display_name = '%s' OR short_name = '%s') AND user_id = '%s'", mysql_real_escape_string($statName), mysql_real_escape_string($statName), mysql_real_escape_string($userID)); $result = mysql_query($query); list($value) = mysql_fetch_row($result); return $value; }
source share