0

I'm trying to get the URL parameter value to the php class and use it WHERE clause in the SQL query.

I have read [GET URL parameter in PHP] and it's helpful but doesn't address Whatever I want. I want to get it inside the PHP class.I read all similar questions in StackOverflow regarding this

Here is the PHP code.

Example URL = https://wacoal/home.php?mo=07 Simply I want to get the above URL mo value to the php class and use it in SQL query

<?php
namespace Phppot;

class Student
{
    private $dbConn;

    public function __construct()
    {
        require_once __DIR__ . '/DataSource.php';
        $this->dbConn = new DataSource();
    }

    function getStudentMark()
    {
      $mo = $_GET['mo'];
      //$query = "SELECT machine_type AS `type`,

       SUM(CASE WHEN `line` = '1' THEN 1 ELSE 0 END) AS `Line 1`,
       SUM(CASE WHEN `line` = '2' THEN 1 ELSE 0 END) AS `Line 2`,
       SUM(CASE WHEN `line` = '3' THEN 1 ELSE 0 END) AS `Line 3`,
       SUM(CASE WHEN `line` = '4' THEN 1 ELSE 0 END) AS `Line 4`,
       SUM(CASE WHEN `line` = '5' THEN 1 ELSE 0 END) AS `Line 5`,
       SUM(CASE WHEN `line` = '6' THEN 1 ELSE 0 END) AS `Line 6`,
       SUM(CASE WHEN `line` = '7' THEN 1 ELSE 0 END) AS `Line 7`,
       SUM(CASE WHEN `line` = '8' THEN 1 ELSE 0 END) AS `Line 8`,
       SUM(CASE WHEN `line` = '9' THEN 1 ELSE 0 END) AS `Line 9`,
       SUM(CASE WHEN `line` = '10' THEN 1 ELSE 0 END) AS `Line 10`
     
FROM   dr_scan
WHERE MONTH(date)='" . $mo . "' 
GROUP  BY machine_type
";
        $result = $this->dbConn->select($query);
        return $result;
    }

I tried it by adding another constrcutor but it gives an error "Cannot redeclare method"

Akshay ak
  • 63
  • 4
  • FYI: Using a direct user input in your query could lead to [SQL injection](https://stackoverflow.com/questions/601300/what-is-sql-injection). Futhermore a class shouldn't relay on the superglobal `$_GET` to execute it's logic. Consider passing the parameter as a function argument – DarkBee Jul 17 '23 at 07:01
  • 2
    Why add another constructor? You also say you read the duplicated post but say you want it in a class. What is the difference? Why don't we see that attempt in your code? – Wimanicesir Jul 17 '23 at 08:54

0 Answers0