无法在php中打印Mysqli查询结果

时间:2016-07-13 08:46:41

标签: php mysqli

我想打印Mysqli查询的结果,但是当我尝试按照以下方式执行时,它不会返回任何值或错误。代码不会通过while循环。我的代码有什么问题,请帮助我!

<?php
    $mysqli = new mysqli("localhost", "root", "", "domains");
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
    $part = explode(".", $str);
    $part1 = $part[0];
    $part2 = $part[1];

    $sql = "SELECT
            DomainCategory.Name
        FROM
            DomainName_Client,
            DomainNameType,
            DomainCategory,
            OrderDomain_Client
        WHERE
            DomainName_Client.Name = '$part1'
        AND DomainNameType.Name = '$part2'
        AND DomainName_Client.TypeID = DomainNameType.ID
        AND DomainCategory.ID = DomainName_Client.DomainCategoryID
        AND OrderDomain_Client.DomainNameID = DomainName_Client.ID";

    $result = $mysqli->query($sql);

    if (!$result = $mysqli->query($sql)) {
        die('There was an error running the query ' . $mysqli->error . ']');
    }
    while ($row = $result->fetch_assoc()) {
        echo 'Total results: ' . $result->num_rows;
    }
?>

1 个答案:

答案 0 :(得分:1)

首先,使用以下代码检查sql查询中返回的结果数,然后使用while或for循环打印它。

echo $result->num_rows;