I'll try to show my search form and continue on bits of public method code.
Here's my form (basic):
<form action="" method="get">
Name:<input type="text" name="name">
Last Name:<input type="text" name="last_name">
Age:<input type="text" name="age">
<input type="submit" value="search" name="search">
</form>
Moving on to my get method:
public function get($table){
$ssql = 'SELECT * FROM '.$table.' WHERE `name` LIKE :name || `last_name` LIKE :last_name';
$name = '%'.$_GET['name'].'%';
$last_name = '%'.$_GET['last_name'].'%';
$stm = $this->dbh->prepare($ssql);
$stm->bindParam(':name',$name,PDO::PARAM_STR);
$stm->bindParam(':last_name',$last_name,PDO::PARAM_STR);
$stm->execute();
echo $stm->rowCount();
while($result = $stm->fetch(PDO::FETCH_OBJ)){
echo $result->name.' '.$result->last_name.'<br>';
}
}
Basically this should give me results based on my $_GET
form fields. My problem is that if I enter name "juris" it does not outputs only 3 rows of juris which are in database but all fields from database. But if I enter lastname and then submit it, it gives out only juris records but then ignores lastname search term.
Shouldn't query like "WHERE etc LIKE "term1" OR etc LIKE "term2" give out results based on OR between 2 given variables? It should separate results if one variable $_get['value']
is given and others are empty?
Got it to work as intended but maybe some one could give a bit cleaner answer how to separate "%%" this wild card?
$ssql = 'SELECT * FROM '.$table.' WHERE (`name` LIKE :name) || (`last_name` LIKE :last_name)';
if(empty($_GET['name'])){
$name = "";
}else {
$name = '%'.$_GET['name'].'%';
}
if(empty($_GET['last_name'])){
$last_name = "";
}else{
$last_name = '%'.$_GET['last_name'].'%';
}
used if statement to separate %% wildcard.