schoolbag Posted September 9, 2008 Share Posted September 9, 2008 Look at this code: <? include("include/session.php"); $korisnik = "cslevente"; function displayBalance(){ global $database; $q = "SELECT username,points FROM users WHERE username='" . $korisnik . "'"; $result = $database->query($q); /* Error occurred, return given name by default */ $num_rows = mysql_numrows($result); if(!$result || ($num_rows < 0)){ echo "Error displaying info"; return; } if($num_rows == 0){ echo "Database table empty"; return; } /* Display table contents */ for($i=0; $i<$num_rows; $i++){ $points = mysql_result($result,$i,"points"); echo "<p>$points</p>"; } } ?> I got a problem with this part: $q = "SELECT username,points FROM users WHERE username='" . $korisnik . "'"; Is there something wrong with it? Because I know that there is a user in the database called cslevente, and if I just put cslevente instead of $korisnik, everything works well. Thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/123443-problem-with-sql/ Share on other sites More sharing options...
JonnoTheDev Posted September 9, 2008 Share Posted September 9, 2008 The variable is outside the scope of the function. You need to pass it in as a parameter: function displayBalance($user){ global $database; $q = "SELECT username,points FROM users WHERE username='" . $user . "'"; } displaybalance("cslevente"); Quote Link to comment https://forums.phpfreaks.com/topic/123443-problem-with-sql/#findComment-637538 Share on other sites More sharing options...
schoolbag Posted September 9, 2008 Author Share Posted September 9, 2008 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/123443-problem-with-sql/#findComment-637543 Share on other sites More sharing options...
schoolbag Posted September 9, 2008 Author Share Posted September 9, 2008 I just tried it out, and it works! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/123443-problem-with-sql/#findComment-637551 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.