Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Posts
    1

    Unanswered: variable not getting passed to sql statement

    Hi...

    I'm a newbie to php and I'm having a problem with passing variables to sql statement. I've looked around the forums and have not found a solution to my problem.

    Here is a test script that I'm trying.


    include("includes/dbconnect.inc");

    $avgscore ="select avg(scores) from scores where scores_type=1 and scores_player_id='$player_id'";

    $avgfront ="select avg(scores) from scores where scores_type=1 and scores_player_id='$player_id' and scores_hole_id < 10";

    //-----------------------------------------
    function get_result($query){
    //-----------------------------------------
    $result = mysql_query($query) OR die ('Query error: '.mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
    mysql_free_result($row);
    }

    foreach (array("$avgscore", "$avgfront", "$avgback") as $querytype) {
    $player_id=1;
    print "<TR>\n";
    print "<TD WIDTH=125>$querytype</TD>\n";
    print "<TD WIDTH=50>" .$output=get_result($querytype). "</TD>\n";
    $player_id=2;
    print "<TD WIDTH=50 ALIGN=CENTER>" .$output=get_result($querytype). "</TD>\n";
    print "</TR>\n";
    }

    since I'm trying to run the same query with just a different player id and then print the result to the screen, I thought I would just set the $player_id variable to the one's that I want, but it is not getting picked up by the sql statement.

    Is it because the sql statement has already been processed because it appears at the beginning of the script ?? It wouldn't be that silly would it.

    remember this is a sample, so I hard coded the player_id in there.

    here is the output from the first print

    select avg(scores) from scores where scores_type=1 and scores_player_id=''

    so as you can see it is coming up blank ??

    any ideas.

    i've tried putting the .$player_id after the equal sign
    i've tried putting it in dbl quotes that were escaped out.

    I know the query is getting called because if i replace the $player_id in the $avgscore query with a 1 or a 2 then it returns the value that I want.

    I'm not passing any variables from other pages or anything.

    essentially what I'm doing is getting all the player id's from the db and then I put that into a a loop and run the php that you see here.

    any help would be great..

    thanx

  2. #2
    Join Date
    Jan 2004
    Location
    India
    Posts
    168

    Re: variable not getting passed to sql statement

    Try this
    $player_id=$HTTP_POST_VARS['$player_id'];
    or
    $player_id=$HTTP_GET_VARS['$player_id'];

    This depends on the methode u r using to send the $player_id.

    Hope ur problem will be solved by this if not revert back to us.

    Regards

    Rajesh
    Originally posted by jodahall
    Hi...

    I'm a newbie to php and I'm having a problem with passing variables to sql statement. I've looked around the forums and have not found a solution to my problem.

    Here is a test script that I'm trying.


    include("includes/dbconnect.inc");

    $avgscore ="select avg(scores) from scores where scores_type=1 and scores_player_id='$player_id'";

    $avgfront ="select avg(scores) from scores where scores_type=1 and scores_player_id='$player_id' and scores_hole_id < 10";

    //-----------------------------------------
    function get_result($query){
    //-----------------------------------------
    $result = mysql_query($query) OR die ('Query error: '.mysql_error());
    $row = mysql_fetch_array($result);
    return $row[0];
    mysql_free_result($row);
    }

    foreach (array("$avgscore", "$avgfront", "$avgback") as $querytype) {
    $player_id=1;
    print "<TR>\n";
    print "<TD WIDTH=125>$querytype</TD>\n";
    print "<TD WIDTH=50>" .$output=get_result($querytype). "</TD>\n";
    $player_id=2;
    print "<TD WIDTH=50 ALIGN=CENTER>" .$output=get_result($querytype). "</TD>\n";
    print "</TR>\n";
    }

    since I'm trying to run the same query with just a different player id and then print the result to the screen, I thought I would just set the $player_id variable to the one's that I want, but it is not getting picked up by the sql statement.

    Is it because the sql statement has already been processed because it appears at the beginning of the script ?? It wouldn't be that silly would it.

    remember this is a sample, so I hard coded the player_id in there.

    here is the output from the first print

    select avg(scores) from scores where scores_type=1 and scores_player_id=''

    so as you can see it is coming up blank ??

    any ideas.

    i've tried putting the .$player_id after the equal sign
    i've tried putting it in dbl quotes that were escaped out.

    I know the query is getting called because if i replace the $player_id in the $avgscore query with a 1 or a 2 then it returns the value that I want.

    I'm not passing any variables from other pages or anything.

    essentially what I'm doing is getting all the player id's from the db and then I put that into a a loop and run the php that you see here.

    any help would be great..

    thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •