-1

我正在制作一个脚本,如果其中一列与关键字匹配,它将为每一行返回数据库中的所有列。例如,在下面的示例中,将返回与单词 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();
4

3 回答 3

2

你问的不是很具体,所以我会广泛地回答这个问题。

您通过迭代一个非常易于使用的mysqli_result对象来循环一个Mysqli查询的结果(使用当前非 End-Of-Live 版本的 PHP 5.4 和 5.5):Traversable

foreach ($result as $row) 
{
    ...

对于 XML 输出,您应该使用该XMLWriter,因为它可以满足您的需求。例如,foreach上面的例子:

foreach($result as $row)
{
    $writer->startElement('sku');

    foreach($row as $name => $value) {
        $writer->startElement($name);
        $writer->text($value);
        $writer->endElement();
    }

    $writer->endElement();
}

完整示例一目了然(我的 SQL 查询和数据库不同,但我猜这不会引起任何头痛):

$mysql = new Mysqli('localhost', 'testuser', 'test', 'test');
if ($mysql->connect_errno) {
    throw new Exception(sprintf("Mysqli: (%d): %s", $mysql->connect_errno, $mysql->connect_error));
}

$sqlQuery = 'SELECT * FROM config';
if (!$result = $mysql->query($sqlQuery)) {
    throw new Exception(sprintf('Mysqli: (%d): %s', $mysql->errno, $mysql->error));
}

header('Content-type: text/xml');
$writer = new XMLWriter();

$writer->openUri('php://output');
$writer->startDocument();
$writer->startElement('results');
$writer->startElement('skus');

foreach($result as $row)
{
    $writer->startElement('sku');

    foreach($row as $name => $value) {
        $writer->startElement($name);
        $writer->text($value);
        $writer->endElement();
    }

    $writer->endElement();
}

$writer->endDocument();

希望这可以帮助。如果您还没有 PHP 5.4,请获取它。如果这是一个问题,您可以将 Mysqli 结果也转换为其他 PHP 版本的迭代器,如我对"PHP mysqli_result: Use Traversable interface with fetch_object"的回答中所述。让我知道这是否会给您带来任何问题。

于 2013-06-30T11:09:54.943 回答
1

我不得不使用 mysqli_fetch_assoc

完成代码:

//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%'";

//run query
if ($result = mysqli_query($db, $sqlQuery)) {

    Header('Content-type: text/xml');

    echo '<?xml version="1.0" encoding="utf-8"?>';

    echo '<results>';

    echo '<skus>';

    //fetch associative array 
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<sku>';

            echo '<id>' . htmlspecialchars($row["id"]) . '</id>';
            echo '<ean>' . htmlspecialchars($row["ean"]) . '</ean>';
            echo '<name>' . htmlspecialchars($row["name"]) . '</name>';
            echo '<description>' . htmlspecialchars($row["description"]) . '</description>';
            echo '<features>' . htmlspecialchars($row["features"]) . '</features>';
            echo '<productHierarchy>' . htmlspecialchars($row["productHierarchy"]) . '</productHierarchy>';
            echo '<url>' . htmlspecialchars($row["url"]) . '</url>';
            echo '<price>' . htmlspecialchars($row["price"]) . '</price>';
            echo '<wasPrice>' . htmlspecialchars($row["wasPrice"]) . '</wasPrice>';
            echo '<deliveryCost>' . htmlspecialchars($row["deliveryCost"]) . '</deliveryCost>';
            echo '<deliveryTime>' . htmlspecialchars($row["deliveryTime"]) . '</deliveryTime>';
            echo '<stockAvailability>' . htmlspecialchars($row["stockAvailability"]) . '</stockAvailability>';
            echo '<skuAvailableInStore>' . htmlspecialchars($row["skuAvailableInStore"]) . '</skuAvailableInStore>';
            echo '<skuAvailableOnline>' . htmlspecialchars($row["skuAvailableOnline"]) . '</skuAvailableOnline>';
            echo '<channel>' . htmlspecialchars($row["channel"]) . '</channel>';
            echo '<manufacturersPartNumber>' . htmlspecialchars($row["manufacturersPartNumber"]) . '</manufacturersPartNumber>';
            echo '<specificationsModelNumber>' . htmlspecialchars($row["specificationsModelNumber"]) . '</specificationsModelNumber>';
            echo '<featuresBrand>' . htmlspecialchars($row["featuresBrand"]) . '</featuresBrand>';
            echo '<imageUrl>' . htmlspecialchars($row["imageUrl"]) . '</imageUrl>';
            echo '<thumbnailUrl>' . htmlspecialchars($row["thumbnailUrl"]) . '</thumbnailUrl>';


        echo '</sku>';

    }

    echo '</skus>';

}

//close the database connection
$db->close();

echo '</results>';
于 2013-06-30T08:41:18.370 回答
-1

$result 返回从查询中找到的每一列的数组。

所以,你只需要像这样循环数组:

//loop through all columns of the result

foreach($result as $key => $value)

{

      // Output : { key : "name", value : "tap" } if "name" === "tap"

      echo "{ key : " . $key . ", result : " + $value + "}";

}

您只需要将您希望的方式插入到 xml 中

于 2013-06-29T23:29:23.163 回答