我正在制作一个脚本,如果其中一列与关键字匹配,它将为每一行返回数据库中的所有列。例如,在下面的示例中,将返回与单词 tap 匹配的所有行。然后我想将结果呈现为 xml。下面的代码似乎有效,totalResults 显示找到的匹配数。如何遍历每个结果并通过 SQL 查询呈现所有匹配的数据?
$query = 'tap';
//connect to the database
$db = new mysqli('localhost', $username, $password, $database );
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
//query the database
$sqlQuery = "select * from skus
where
`id` like '%$query%' OR
`name` like '%$query%' OR
`description` like '%$query%' OR
`ean` like '%$query%' OR
`price` like '%$query%' OR
`wasPrice` like '%$query%' OR
`deliveryCost` like '%$query%' OR
`deliveryTime` like '%$query%' OR
`stockAvailability` like '%$query%' OR
`skuAvailableInStore` like '%$query%' OR
`skuAvailableOnline` like '%$query%' OR
`channel` like '%$query%' OR
`manufacturersPartNumber` like '%$query%' OR
`specificationsModelNumber` like '%$query%' OR
`featuresBrand` like '%$query%' OR
`imageUrl` like '%$query%' OR
`thumbnailUrl` like '%$query%' OR
`features` like '%$query%' OR
`url` like '%$query%' OR
`productHierarchy` like '%$query%'";
if(!$result = $db->query($sqlQuery)){
die('There was an error running the query [' . $db->error . ']');
}
Header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<totalResults>Total results: ' . $result->num_rows . '</totalResults>';
//close the database connection
$db->close();