1

I have a function that takes an SQL query and parameters/array of parameters. This function working fine on UPDATE statements but not on SELECT statements.The function mysqli_fetch_array is not returning anything and I cannot figure out why. The error message I am getting is;

"mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given"

I am using PHP version 5.5.

The query is as follows:

<?php

        $txtSearch = isset($_POST["txtSearch"]) ? $_POST["txtSearch"] : "";

            $result_search = null;
            $query = "";
            if(!empty($txtSearch))
            {
                if($page == "adverts")
                {
                    $query = "
                        SELECT
                            A.ID,
                            A.title,
                            A.productname,
                            A.price,
                            A.description,
                            A.productlocation,
                            (SELECT I.name FROM image I WHERE I.advertID = A.ID LIMIT 1) AS name
                        FROM advert A
                        WHERE A.title LIKE ?";
                }
                $result_search = queryDB($query, array("s", "%".$txtSearch."%"));
            }
           while($row = mysqli_fetch_array($result_search))
                {

                                                echo 
                                                    "<div class='post_container'>
                                                            <div class='title_banner'>".$row["title"]."</div>
                                                            <div>£".$row["price"]."</div>
                                                    </div>";
                                            }                           
        ?>
user3034845
  • 125
  • 1
  • 1
  • 10

2 Answers2

0

Try to check query result before you're passing it as an argument. If it returns false, than there is something wrong with the query.

if($result_search === FALSE) { 
    die(mysql_error()); // TODO: better error handling
}

And I would change your query on something like this:

SELECT
    A.ID, A.title, A.productname,
    A.price, A.description,
    A.productlocation,
    I.name
FROM 
    advert A
INNER JOIN
    image I ON I.advertID = A.ID
WHERE 
    A.title LIKE ?"
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jozhe
  • 197
  • 10
  • 1
    Why should the OP change the query to something that does something different? The query in the question is (presumably) correct and the problem lies elsewhere. – Gordon Linoff Jan 10 '16 at 13:42
  • That is true, it could be different. If you use this query OP, use it with caution. Result depends on your database design. If not sure, it's better to use your previous query. – jozhe Jan 10 '16 at 15:31
0

You could return $stmt->get_result()instead, which will provide you with a result you can iterate over.

Your error tells you that you are returning a boolean, not a mysqli_result. The documentation on mysqli_stmt::fetch() confirms that it returns a boolean.

HenryTK
  • 1,287
  • 8
  • 11
  • get_result() is giving a new error - "Call to undefined method mysqli_stmt::get_result()" – user3034845 Jan 10 '16 at 11:08
  • I apologise, I didn't mean to insert it directly into your code. I meant it will give you something that you can iterate over. Click the link to the documentation on `$stmt->get_result()` and you will see what I mean. – HenryTK Jan 10 '16 at 11:10