-1

I want to verify if it's more than one result.

Code:

        <?php
    if (!isset($_GET["q"])) {
        echo "";
    } else {
        $res = "%{$_GET["q"]}%";
        $sql = "SELECT * FROM announce WHERE title LIKE ? ";
        $stmt = mysqli_stmt_init($conn);
        mysqli_stmt_prepare($stmt, $sql);
        mysqli_stmt_bind_param($stmt, "s", $res);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        if ($result > 0) {
            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                echo '<a href="./invideo.php?movieID=' . $row["idAnn"] . '">' . $row["title"] . '</a>';
            }
        } else {
            echo "0 results";
        }

    }
    ?>

But get this error:

Notice: Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\site\search_page.php on line 110

Problem:

$result = mysqli_stmt_get_result($stmt);

if ($result > 0)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Daciu
  • 29
  • 3
  • Which line is line 110? – Felippe Duarte Oct 16 '20 at 12:13
  • if ($result > 0) { – Daciu Oct 16 '20 at 12:22
  • Use fetch_all() – Dharman Oct 16 '20 at 12:25
  • 1
    Well, `$result` isn't a number (and can't be interpreted as a number either). That's what the error is telling you. So you can't compare it to 0, because it makes no logical sense. I think you're looking for the num_rows function - see https://www.php.net/manual/en/mysqli-stmt.num-rows.php . (And if you'd read the manual for get_result at https://www.php.net/manual/en/mysqli-stmt.get-result.php you'd see it tells you what it returns, and it clearly isn't a number - _"Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure_".) – ADyson Oct 16 '20 at 12:33

1 Answers1

0

Use mysqli_num_rows($result) > 0 or $result->num_rows > 0 instead of $result > 0

Dmytro Huz
  • 1,514
  • 2
  • 14
  • 22
  • https://www.php.net/manual/en/mysqli-stmt.num-rows.php it is the same that $result->num_rows. what do you think about that? – Dmytro Huz Oct 16 '20 at 13:17
  • yes, You are totally right. Thank you for paid attention to that. I fixed my answer. – Dmytro Huz Oct 16 '20 at 13:29
  • Isn't this the same now as the proposed duplicate? – Dharman Oct 16 '20 at 13:29
  • yes, but I added the answer earlier that it was marked as duplicate. So it will be much more useful to have here the correct answer instead of just delete it. – Dmytro Huz Oct 16 '20 at 13:30
  • The duplicate was easily searchable. In fact there are two duplicates that would suit to answer this question. Have you searched for existing answers at all before answering? Adding the same answer again is not going to be very useful. – Dharman Oct 16 '20 at 13:32
  • No, I didn't. If it is easy, why does TS didn't find it? Not sure what are you trying to prove? I helped him with his particular problem instead of wasting the time for searching duplicates. Have you searched for them when writing the comment to the question? Why you just didn't send the link to duplicate in the comment? – Dmytro Huz Oct 16 '20 at 13:39
  • 1
    I am trying to explain to you that answering a duplicate question is not appreciated on Stack Overflow. We provide answers by linking to existing ones. I don't know what TS is but you should try linking to an existing answer whenever possible. Only answer if it is a new interesting problem. Remember we are not here to answer every question and help the question asker. This is not a help desk. We maintain a repository of easy to find answers to programming questions. Asking the same question again is just making it more difficult to find the correct answer. – Dharman Oct 16 '20 at 13:43
  • 1
    FYI I am at work so I didn't have time to vote to close, and when I came back the question was already closed. Searching for duplicates is not a waste of time. On contrary, answering the same question again is a waste of time. – Dharman Oct 16 '20 at 13:44