Question
Lets say I have a table that consists field name= Jack, age=17, address= UK For now the code only read first field (name) and wont
Lets say I have a table that consists field name= Jack, age=17, address= UK
For now the code only read first field (name) and wont read if i enter a single age or address field.
Search: "Jack" - data is shown Search: "UK" - data is not shown
How to search the keywords based on the field either name, age and address or all at once.
$con = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
if(isset($_POST["submit"])) { $search = $_POST["searchname"]; $search = strtoupper($search); $search = strip_tags($search); $search = trim($search);
$arr = explode(' ', $search); $arr_condition = array(); $arguments = array(); foreach ($arr as $ar) { $arr_condition[] = 'name LIKE ? ' AND 'age LIKE ? ' AND 'address LIKE ? ' ; $arguments[] = '%'.$ar.'%'; }
$sth = $con->prepare('SELECT * FROM `tableuser` WHERE '.implode(' OR ', $arr_condition));
$sth->setFetchMode(PDO:: FETCH_OBJ); $sth -> execute($arguments); $result = $sth -> fetchAll();
if ($sth->rowCount()>0) { foreach($result as $row) { ?>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started