user1793864
user1793864

Reputation: 185

Oracle Express Edition and PHP

I have connected an Oracle express edition database with PHP code, I need to select data from a table between two dates my code is as below. Please help me to retrieve the data.

when selecting the data between two date ranges I got the following error.

Warning: oci_execute(): ORA-00911: invalid character in C:\wamp\www\trades\dat.php on line 14

Warning: oci_fetch_array(): ORA-24374: define not done before fetch or execute and fetch in C:\wamp\www\trades\dat.php on line 18

    <html>
    <body>
 <?php

 $dt1 = $_POST["bday1"];
 $dt2 = $_POST["bday2"];

 // Create connection to Oracle
 $conn = oci_connect("cse", "mahesh123", "XE");

 $query = 'select * from daily_trades where trdt= $dt1';
 $stid = oci_parse($conn, $query);
 $r = oci_execute($stid);

 // Fetch each row in an associative array
 print '<table border="1">';
 while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
 print '<tr>';
 foreach ($row as $item) {
 print '<td>'.($item !== null ? htmlentities($item, ENT_QUOTES) : 
 '&nbsp').'</td>';
 }
 print '</tr>';
 }
 print '</table>';

 ?>


 </body>
 </html>

Upvotes: 0

Views: 230

Answers (2)

Jay Shankar Gupta
Jay Shankar Gupta

Reputation: 6088

As you have to pass date as string so put single ' around $dt1

$query = "select * from daily_trades where trdt= TO_DATE('$dt1',  'YYYY-MM-DD')";

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57141

You will most likely need to convert the string into a date using TO_DATE(), depending on the format depends on the pattern you give...

$query = "select * from daily_trades 
            where trdt = TO_DATE('$dt1',  'YYYY-MM-DD')";

But also if you want the dates between two dates, you should use BETWEEN.

$query = "select * from daily_trades 
           where trdt between TO_DATE('$dt1',  'YYYY-MM-DD') 
                          and TO_DATE('$dt2',  'YYYY-MM-DD')";

Just make sure you get the dates the right way round (earliest to latest).

Upvotes: 2

Related Questions