user984264
user984264

Reputation: 35

Php postgresql search query

I am not sure if I am doing the query right but i am just a beginner in PHP and postgresql hence my code.

what i am trying to accomplish is a search using date picker which will give data from a date:

<?php
$output = '';
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];

    $query = ("SELECT trees.tree_type,tree_solds.transaction_id,tree_solds.actual_height,tree_solds.selling_height,tree_solds.sub_total,transactions.date_purchased FROM tree_solds
                                                    left join trees on tree_solds.tree_id = trees.id
                                                    left join transactions on transactions.id = tree_solds.transaction_id
                                                    WHERE date_purchased LIKE $searchq ");


                                        $result = pg_query($query); 
                                        if (!$result) { 
                                            echo "Problem with query " . $query . "<br/>"; 
                                            echo pg_last_error(); 
                                            exit(); 
                                        } 


                        $count = pg_num_rows($result);
                        if ($count == 0) {
                            $output = 'No Data on that date!';
                        } else {
                            while ($row = pg_fetch_array($result)) {
                               $output .= printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", htmlspecialchars($myrow['transaction_id']), htmlspecialchars($myrow['date_purchased']), htmlspecialchars($myrow['tree_type']), htmlspecialchars($myrow['actual_height']), htmlspecialchars($myrow['selling_height']), number_format($myrow['sub_total'], 2));
                            }
                        }
                    }
                ?>

HTML form

 <form action="location4.php" method="post">
 <input type="text" class="span2" name="search" value="" data-date-format="yyyy-mm-dd" id="dp2">

 <button type="submit" class="btn btn-default">Submit</button>
 </form>
 <?php print("$output");?>

Keep getting this error and no result.

Warning: pg_query(): Query failed: ERROR: operator does not exist: timestamp without time zone ~~ integer LINE 4: ... WHERE date_purchased LIKE 2014-... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. in /Applications/MAMP/htdocs/xmastool/location4.php on line 50 Problem with query SELECT trees.tree_type,tree_solds.transaction_id,tree_solds.actual_height,tree_solds.selling_height,tree_solds.sub_total,transactions.date_purchased FROM tree_solds left join trees on tree_solds.tree_id = trees.id left join transactions on transactions.id = tree_solds.transaction_id WHERE date_purchased LIKE 2014-12-07 ERROR: operator does not exist: timestamp without time zone ~~ integer LINE 4: ... WHERE date_purchased LIKE 2014-... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Upvotes: 1

Views: 754

Answers (3)

user984264
user984264

Reputation: 35

solved the issue due to the tip of polka_bolka.

 WHERE TO_CHAR(date_purchased, 'yyyy-mm-dd') LIKE '%$searchq%' ")

converted the date_purchased to char.

<?php
$output = '';
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];

    $query = ("SELECT trees.tree_type,tree_solds.transaction_id,tree_solds.actual_height,tree_solds.selling_height,tree_solds.sub_total,transactions.date_purchased FROM tree_solds
                                                    left join trees on tree_solds.tree_id = trees.id
                                                    left join transactions on transactions.id = tree_solds.transaction_id
                                                    WHERE TO_CHAR(date_purchased, 'yyyy-mm-dd') LIKE '%$searchq%' ");


                                        $result = pg_query($query); 
                                        if (!$result) { 
                                            echo "Problem with query " . $query . "<br/>"; 
                                            echo pg_last_error(); 
                                            exit(); 
                                        } 


                        $count = pg_num_rows($result);
                        if ($count == 0) {
                            $output = 'No Data on that date!';
                        } else {
                            while ($row = pg_fetch_array($result)) {
                               $output .= printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", htmlspecialchars($row['transaction_id']), htmlspecialchars($row['date_purchased']), htmlspecialchars($row['tree_type']), htmlspecialchars($row['actual_height']), htmlspecialchars($row['selling_height']), number_format($row['sub_total'], 2));
                            }
                        }
                    }
                ?>

Upvotes: 0

tsnorri
tsnorri

Reputation: 2097

Try this

pg_query_params('SELECT ... WHERE DATE_TRUNC('day', date_purchased) = $1', array($searchq))

See [the documentation for details. You should always make sure that query parameters are properly escaped (i.e. not concatenated verbatim to the query) to prevent SQL injection.

Upvotes: 0

vaso123
vaso123

Reputation: 12391

Use like this: WHERE date_purchased LIKE '%$searchq%' if you want just a part of it.

If you want exact match then WHERE date_purchased = '$searchq'

NOTE: % is a joker character in the LIKE, check the manual how to use.

Upvotes: 0

Related Questions