0

I am not sure if this is possible, but I'm trying to build a function that can build MYSQL queries.

It has a base query. If URL has a get value (script.php?game=AGame) it will take that get variable and rename it. The script will then append the required data needed to complete the MYSQL query accordingly.

    function build_query($getvar, $renamevar, $dbfield){
        $query = "SELECT * FROM release_dates ";
        $arguments = 0;

        if(isset($_GET['$getvar'])){
        $renamevar = $_GET['$getvar'];

        if($arguments >= 1){
                $query .= "AND  '$dbfield' = '$renamevar' ";
                $arguments = $arguments +1;
            }
            else{
                $query .= "'$dbfield' = '$renamevar' ";
            }
        }
    }

    build_query(g, game, game_title);

However, when running the code I get the following errors:

Notice: Use of undefined constant game - assumed 'game' in /www/sites/164/index.php on line 46

Notice: Use of undefined constant g - assumed 'g' in /www/sites/164/index.php on line 46

Notice: Use of undefined constant game_title - assumed 'game_title' in /www/sites/164/index.php on line 46

Any help would be appreciated.

  • 1
    It is possible when you realize 3 things: 1. Associative array key type is a string 2. Try to `var_dump` what you pass as a key first 3. string literals are enclosed in either single or double quotes – zerkms Oct 29 '14 at 21:00
  • and 4. fieldnames are not enclosed in single quotes. Either nothing, or backticks. I.e. ` – developerwjk Oct 29 '14 at 21:03
  • 1
    You're code is vulnerable to SQL Injection attacks. Please research this topic before writing production code that talks to databases. – DampeS8N Oct 29 '14 at 21:04
  • 5. Also your logic inside the function is wrong,$arguments will always be 0 as it is now. 6. You "build" the query but do not use/return it in any way. 7. Please revisit the whole thing. – Kypros Oct 29 '14 at 21:22

1 Answers1

0

Call Function this

 build_query('g', 'game', 'game_title');
Harutyun Abgaryan
  • 2,013
  • 1
  • 12
  • 15