I have an autofill search box with a PHP script that gets data from the DB. It uses a PDO connection, and it works perfectly even with my WordPress Database, but since I'm not sure of how PDO works, I'd like to rewrite this with WordPress functions. Does anyone know how?
This is the autosuggest script:
if (isset($_GET['term'])){
$return_arr = array();
try {
$conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD."");
$conn->exec("set names utf8");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT supplier_company FROM wp_teleapo_supplier');
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
while($row = $stmt->fetch()) {
$return_arr[] = $row['supplier_company'];
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
}