Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/123443-problem-with-sql/
Share on other sites

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");

Link to comment
https://forums.phpfreaks.com/topic/123443-problem-with-sql/#findComment-637538
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.