-1

I want to display data book but the problem is i got this error

 Notice: Only variables should be passed by reference in C:\xampp\htdocs\library-api\master.php on line <i>259</i></th></tr>

and here is my master.php

public function GetReportBook($id_book,$TanggalStart, $TanggalEnd)
{
    
    $sqlsrvquery = "
    EXEC [dbo].[GetReportBook] 
    @id_book = ?, 
    @TanggalStart = ?,
    @TanggalEnd = ?";
    $stmt = $this->conn->prepare($sqlsrvquery);
    $stmt->bindParam(1, $id_book, PDO::PARAM_STR);
    $stmt->bindParam(2, date('Ymd', strtotime($TanggalStart)), PDO::PARAM_STR);
    $stmt->bindParam(3, date('Ymd', strtotime($TanggalEnd)), PDO::PARAM_STR);
    $stmt->execute();
    if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $report_arr = array(
            "status" => true,
            "id_book" => $row['id_book'],
            "book_name" => $row['book_name'],
            //etc
        );
    } else {
        $report_arr = array(
            "status" => false,
            "message" => "Invalid Get Report Book!",
        );
    }
    print_r(json_encode($report_arr));
}

any solution of this? because i think the error came because of date time

ADyson
  • 57,178
  • 14
  • 51
  • 63
Blanc Chen
  • 418
  • 4
  • 15
  • 1
    Check out the [documentation](https://www.php.net/manual/en/pdostatement.bindparam) of `bindParam`. The second argument is passed by reference. What you're passing for `2` and `3` is not a variable, but an expression. Store them in variables and pass them. – El_Vanja Jan 21 '21 at 10:19
  • Just put your 2 dates into a variable before calling the bindParam and then use them in the query – RiggsFolly Jan 21 '21 at 10:21

1 Answers1

1

You're passing an expression instead of a variable in the 2nd and 3rd calls to bindParam. As the notice says, only variables should be used when passing by reference. And the bindParam documentation makes clear that the second argument to that function is passed by reference.

So, try it like this:

$startStr = date('Ymd', strtotime($TanggalStart));
$endStr = date('Ymd', strtotime($TanggalEnd));
$stmt->bindParam(2, $startStr, PDO::PARAM_STR);
$stmt->bindParam(3, $endStr, PDO::PARAM_STR);

(N.B. This isn't specific to dates and times, as you mentioned - using any expression would cause the same result.)

ADyson
  • 57,178
  • 14
  • 51
  • 63