-1

Guys, I want to correct this function the request from DB its works fine but I want to use it as a function so I can repeat it 30 times

the problem here is I don't know really how to use it into a function

<?php

        $today = date("Y-m-d") ;

        function getDateFunc($today) {

        $getttlprice = "SELECT SUM(price) AS TotalPrice FROM orders WHERE ldate = :today";
        $ttp = $pdo->prepare($getttlprice);
        $ttp->execute(array(':today'=>$today));
        $resultttp =  $ttp->fetchObject();
        $todayresulte = $resultttp->TotalPrice;
        echo $todayresulte ;
        }

        getDateFunc('16-06-2018') ;

     ?>

normally the function is like this but I don't know how to use it with my requst

function getDateFunc($today){
  echo $today ; // resulte 16/06/2018 by today time
}

enter image description here

  • i think that you are not giving connection – vivek modi Jun 16 '18 at 20:14
  • There is no database connection tool in the function. You can call the database connection code with `global`. Also your date format appears to be incorrect. Today: _year, month and day_ but yor write; _day, month and year_ – Sahipsiz Jun 16 '18 at 20:15
  • It is not advised to use the `global` keyword. Instead I suggest using a wrapper class to put your function into as a public method and pass the instantiated PDO connection object to the constructor. – Will B. Jun 16 '18 at 20:25
  • For simplicity sake you would need to pass your `$pdo` variable as an argument to your function so that is accessible without using the `global` keyword. ie. `function getDateFunc($today, $pdo){ /*...*/ }` Then use it `require_once 'connect.php'; getDateFunc('2018-06-16', $pdo);` – Will B. Jun 16 '18 at 20:57

2 Answers2

0
try {
$pdo= new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully"; 
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}

you have to give connection in $pdo

vivek modi
  • 800
  • 2
  • 7
  • 23
0

There is no database connection constructor in the function. You can call the database connection code with global or in the function.

Also your date format appears to be incorrect.

Today: year, month and day but you write; day, month and year

 function getDateFunc($today) {

     try {
        $pdo= new PDO("mysql:host=$servername;dbname=myDB", $username, $password); 
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully"; 
        }
        catch(PDOException $e)
        {
            echo "Connection failed: " . $e->getMessage();
        }


    $getttlprice = "SELECT SUM(price) AS TotalPrice FROM orders WHERE ldate = :today";
    $ttp = $pdo->prepare($getttlprice);
    $ttp->execute(array(':today'=>$today));
    $resultttp =  $ttp->fetchObject();
    $todayresulte = $resultttp->TotalPrice;
    echo $todayresulte ;
}
Sahipsiz
  • 105
  • 1
  • 11