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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!