skip to Main Content

I am wanting to select between two options for an SQL query from user supplied parameters.

The code is below. I’ve tried using the &(ampersand) in front of the variable but to no avail. The query works if I manually set $select to either $full or $summary, so I know that the SQL query is correct.

data.php

<?php

function chartGrid($stdate, $enddate, $select ){

include 'login.php';
include 'grid.php';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=charts", $username, $password);
      

    $sql = $select;   
            
    $stmt = $dbh->prepare($sql);
    $stmt->execute();

    foreach($stmt as $row) {
       $result = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
       print($result);
       exit;
       }

    /*** close the database connection ***/
    $dbh = null;
}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
 }  
 
chartGrid('2015-10-12', '2016-10-11', $full ); 

?>

grid.php

<?php

$full = "SQL SELECT full details from $stdate to $enddate";

$summary = "SQL SELECT summary details from $stdate to $enddate";

?>

What am I missing?

2

Answers


  1. # Make grid.php an associative array
    
    $query = array(
        'full' => "SELECT full details FROM table_name WHERE date BETWEEN :stdate AND :enddate",
        'summary' => "SELECT summary details FROM table_name WHERE date BETWEEN :stdate AND :enddate"
    );
    
    # In data.php call the function with desired key
    
    function chartGrid($stdate, $enddate, $select) {
        include 'login.php';
        include 'grid.php';
    
        try {
            $dbh = new PDO("mysql:host=$hostname;dbname=charts", $username, $password);
    
            // Select the SQL query based on the $select parameter
            $sql = $query[$select];
    
            $stmt = $dbh->prepare($sql);
            $stmt->bindParam(':stdate', $stdate);
            $stmt->bindParam(':enddate', $enddate);
            $stmt->execute();
    
            // Fetch and encode the result
            $result = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
            print($result);
    
            /*** close the database connection ***/
            $dbh = null;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    // Call the function with the desired parameters
    chartGrid('2015-10-12', '2016-10-11', 'summary');
    
    Login or Signup to reply.
  2.     include 'login.php';
        
        try {
            $dbh = new PDO("mysql:host=$hostname;dbname=charts", $username, $password);
            
            // Define SQL queries based on $select
            if ($select == 'full') {
                $sql = "SQL SELECT full details from $stdate to $enddate";
            } elseif ($select == 'summary') {
                $sql = "SQL SELECT summary details from $stdate to $enddate";
            } else {
                // Handle invalid $select value
                echo "Invalid selection";
                return;
            }
            
            $stmt = $dbh->prepare($sql);
            $stmt->execute();
    
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $result_json = json_encode($result);
            print($result_json);
        } catch(PDOException $e) {
            echo $e->getMessage();
        } finally {
            // close the database connection
            $dbh = null;
        }
    }
    
    chartGrid('2015-10-12', '2016-10-11', 'full');
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search