Jump to content

Problem with SQL


schoolbag

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.