0

I have a simple piece of text from a form I want to pass to a placeholder and it works on other pieces of text. For whatever reason when I search with the word "fun", the script fails.

Here is my code. Normally, this is straight forward, but I can't seem to wrap my head around this one. Any thoughts are appreciated.

    $search = $_POST['searchInput'];
    $search = strtolower($search);
    $search = '%'.$search.'%';

    try {
        $sqlSearch = "SELECT items.item_id,item_detail,auc_id,item_desc,categories,min_bid_increment,auc_start,auc_end,display_item,bidder,amount_bid FROM items
                LEFT JOIN bid_history ON items.item_id = bid_history.item_id
        WHERE org_id = :orgid
        AND auc_id NOT LIKE 'live%'
        AND display_item = '1'
        AND item_detail LIKE :searchInput
        OR item_desc LIKE :searchInput
        OR items.item_id LIKE :searchInput
        ORDER BY items.item_id ASC, bid_history.amount_bid DESC;";
        $sSearch = $pdo->prepare($sqlSearch);
        $sSearch->bindValue(':orgid',$org_id);
        $sSearch->bindValue(':searchInput',$search);
        $sSearch->execute();
        $resultsSearch = $sSearch->fetchAll();
    }

    catch (PDOException $e) {
        echo $e;
    }

EDIT:

If I manually put this query into my database, I get results back.

SELECT items.item_id,item_detail,auc_id,item_desc,categories,min_bid_increment,auc_start,auc_end,display_item,bidder,amount_bid FROM items 
    LEFT JOIN bid_history ON items.item_id = bid_history.item_id
    WHERE org_id = 'CHS102915' 
    AND auc_id NOT LIKE 'live%' 
    AND display_item = '1' 
    AND item_detail LIKE '%fun%'
    OR item_desc LIKE '%fun%'
    OR items.item_id LIKE '%fun%'
    ORDER BY items.item_id ASC, bid_history.amount_bid DESC
wowzuzz
  • 1,398
  • 11
  • 31
  • 51
  • I think you should use :searchInput only one time in your bindValue. So you can use ```:searchInput1, $search``` and ```searchInput2, $search``` and ```searchInput3, $search``` – ChoiZ Nov 12 '14 at 15:13
  • Okay, I will give that a try. – wowzuzz Nov 12 '14 at 15:14
  • Doesn't seem to make any difference when I use three different placeholders. – wowzuzz Nov 12 '14 at 15:15
  • What error do you get? Do a print_r($sSearch->errorInfo()); – Naruto Nov 12 '14 at 15:18
  • I am passing these results back as json, all I get back on my console is 500 internal server error. – wowzuzz Nov 12 '14 at 15:20
  • Add error reporting to the top of your file(s) right after your opening `setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened, if you're not already doing so. Check your logs also. – Funk Forty Niner Nov 12 '14 at 15:21
  • Thanks Fred, I already have my setAttribute set in another include file for my connection. I will try the error_reporting method you mentioned. – wowzuzz Nov 12 '14 at 15:22
  • You're welcome. Also this `catch (PDOException $e) { echo $e; }` do `catch(PDOException $e) { print $e->getMessage(); }` – Funk Forty Niner Nov 12 '14 at 15:23
  • I tried the above solutions, has not worked. I did comment out the $resultsSearch = $sSearch->$fetchAll(); and the error is not showing in the console now, but I need that particular line of code to collect my results. – wowzuzz Nov 12 '14 at 15:28
  • Longshot, since I wasn't able to read carefully - but I did notice that you have `;` in your `$sqlSearch` at the end of the query. Perhaps removing it would make the issue go away? – N.B. Nov 12 '14 at 15:31
  • Odd enough as it is, that semicolon actually got my error to go through. This is what I am seeing now for my error. Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 71 bytes) in items.html.php on line 270 – wowzuzz Nov 12 '14 at 15:36
  • You're simply receiving too many records, you should probably limit that a bit, or if you really need so many records - increase allowed memory limit of PHP. – N.B. Nov 12 '14 at 15:41
  • Try getting rid of `$search = '%'.$search.'%';` and do `$sSearch->bindValue(':searchInput','%'.$search.'%');` – Funk Forty Niner Nov 12 '14 at 15:41
  • However, my above comment may need to be executed as an array. I pulled that off from http://stackoverflow.com/a/1786656/ - `$search = '%'.$search.'%';` seems to be the faulty line and may need to be adjusted somehow. – Funk Forty Niner Nov 12 '14 at 15:49
  • I believe it's more of a sql issue now with the amount of records I have coming in. – wowzuzz Nov 12 '14 at 15:53

1 Answers1

0

The problem end up being that the query I was using was grabbing too many records which ended up giving me a memory error that would crash the script. How I corrected this was, was to have a more efficient query. Always set your error indicators up as Fred explained above in the comments and thanks for the semicolon issue at the end N.B. So in the end it had nothing to do with my wildcards or placeholder, just a query that needed improvement.

wowzuzz
  • 1,398
  • 11
  • 31
  • 51