3

The point is to make a query that will grab values introduced by the user on a input box, and retrieve the database records found trough that keyword comparison.

On a innodb engine, so no MATCH AGAINST available correct ? I will use LIKE on a indexed column table, hope it's ok.

traditionally we will do:

SELECT our_column FROM our_db_table WHERE our_column LIKE '%$queryString%';

So if our query string is AB we will retrieve both: "lab" and "abnormal" precise?

1) How can we achieve this but, by using PDO ?


Thinking:

Something like,

$stmt = $this->_dbh->prepare("SELECT d.our_column FROM our_db_table d WHERE d.our_column LIKE ?");

But what's next?

Normally I would do:

$stmt->bindParam(1, $ourTableVo->getOurColumn(), PDO::PARAM_STR, 255);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_OBJ);

2) Could a VO be of any use on this case?

Thanks a lot in advance!

Aldwoni
  • 1,168
  • 10
  • 24
MEM
  • 30,529
  • 42
  • 121
  • 191

1 Answers1

3
$stmt->bindValue(1, '%' . $ourTableVo->getOurColumn() . '%', PDO::PARAM_STRING);

Wouldn't this work?

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • I don't know. It seems logical... But I noticed that you have used bindValue instead of bindParam... any particular reason for that? :) Thanks a lot in advance – MEM Aug 26 '10 at 15:21
  • Only variables can be bound using `bindParam` (I actually wonder why your function return value may be bound. Or does it return by reference?) If you want to bind a value, not a variable you must use `bindValue`. – NikiC Aug 26 '10 at 15:45
  • Ok. Until now, I have used bindParam to bound the returned value of my getters. With success. I'm probably misunderstanding something? I don't know if it returns by reference. :( I'm quite newbie. :s How can I see that? I will be glad to answer! – MEM Aug 26 '10 at 15:58
  • @nikic please stay with me on this... :) If I do $stmt->bindParam(1, '%' . $dominioVo->getNome() . '%', PDO::PARAM_STR, 255); I get: "Fatal error: Cannot pass parameter 2 by reference" What does this error means? Yes, it's related to what you have told already but I'm not connecting the dots, please have patience... :) – MEM Aug 26 '10 at 16:25
  • Ok, the above will not work but this seems to do it: $stmt->bindValue(1,'%' . $ourTableVo->getOurColumn() . '%', PDO::PARAM_STR); It seems that, with bindValue we can only have 3 params instead of 4. So the last param indicating the limit of our "PARAM_STR" seems to NOT be valid. If you edit your question, I can mark it as answered. The fact that I don't understand the question, is, indeed, another question. :) – MEM Aug 26 '10 at 17:00
  • 1
    In this example: `'%' . $ourTableVo->getOurColumn() . '%'` is a result of an expression, thus a value. If you had written `$var = '%' . $ourTableVo->getOurColumn() . '%'` than it would be a variable and could have been bound using `bindParam`: `$stmt->bindParam(1, $var = '%' . $ourTableVo->getOurColumn() . '%', PDO::PARAM_STRING);` So, general rule: If you want to use the preared statement only once you should always use `bindValue`, so there aren't any problems ;) – NikiC Aug 26 '10 at 19:09
  • Thanks a lot for your comments. So, bindParam binds a variable to a given placeholder. That variable is not evaluated at that time, instead, it is bind by reference. This means that, since it's not evaluated at that time, we actually don't have access to that variable value, so, we can't concatenate nothing to it. Hence, we end up getting a fatal error. - precise? – MEM Aug 27 '10 at 10:51