PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE info ( id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (id) ); INSERT INTO info(id, name) VALUES (1, 'Chad'), (2, 'Ned'), (3, 'Dave'), (4, 'Newt'), (5, 'Bill'), (6, 'Norton'), (7, 'Alan');
Copy Clear
Copy Format Clear
<?php // $mysqli is already declared as a working database connection by this sandbox $string = "Bill N_d Dave"; $conditions = []; $parameters = ['']; foreach (array_unique(explode(' ', $string)) as $value) { $conditions[] = "name LIKE ?"; $parameters[0] .= 's'; // $value = addcslashes($value, '%_'); // if you want to make wildcards from input string literal. https://stackoverflow.com/questions/18527659/how-can-i-with-mysqli-make-a-query-with-like-and-get-all-results#comment132930420_36593020 $parameters[] = "%{$value}%"; } // $parameters now holds ['sss', '%Bill%', '%N_d%', '%Dave%'] $query = "SELECT * FROM info"; if ($conditions) { $stmt = $mysqli->prepare($query . ' WHERE ' . implode(' OR ', $conditions)); $stmt->bind_param(...$parameters); $stmt->execute(); $result = $stmt->get_result(); } else { $result = $conn->query($query); } foreach ($result as $row) { echo "<div>{$row['name']}</div>\n"; }
Show:  
Copy Clear