Chathurika
Chathurika

Reputation: 419

Could not be able to print Mysqli query result in php

I want to print result of a Mysqli query, But when I try to do as following way, It does not return any values or error. The code does not go through the while loop. What would be the wrong with my code, Please help me!

<?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;
    }
?>

Upvotes: 1

Views: 76

Answers (1)

Rajkumar R
Rajkumar R

Reputation: 1097

First you check the number of results returning in the sql query using the following code and after that you print it using while or for loop.

echo $result->num_rows; 

Upvotes: 1

Related Questions