views:

36

answers:

1

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!

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

Wouldn't this work?

nikic
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
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
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
@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
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
Thanks for the fix, I edited my answer.As concerning the difference between `bindParam` and `bindValue`: `bindParam` binds a reference to a *variable*. That is, if you bind `$a` using `bindParam` and than change `$a` then the bound value will change too. This is especially helpful, if you are `execute`ing the prepared statement several times. `bindValue` on the contrary binds a *value*, not a variable. If you `bindValue` `$a` and then change `$a` the bound value *does not* change.
nikic
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
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