-1

I am getting this error "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, array given in" out of code snippet "searchcar.php"

$modelmake = $_POST['model_make'];
$result = $db->select('car_information','*', 'Model_Make LIKE \'%$modelmake%\''); 
while($row = mysqli_fetch_assoc($result))
{
    echo 'Model'.$row['model_make'];
}

here is code snippet from "database.php" the select function

 public function select(
        $table,
        $fields = '*',
        $where = '1=1',
        $order = '',
        $limit = '',
        $desc = false,
        $limitBegin = 0,
        $groupby = null,
        $monitoring = false
    ) //monitoring is set to true to view the actual query
    {
//  $query ='SELECT ' . $fields . ' FROM ' . $table ;
    $query = 'SELECT ' . $fields . ' FROM ' . $table . ' WHERE ' . $where;

        if (!empty($groupby)) {
            $query .= ' GROUP BY ' . $groupby;
        }

        if (!empty($order)) 
        {
            $query .= ' ORDER BY ' . $order;

            if ($desc) 
            {
                $query .= ' DESC';
            }
        }

        if (!empty($limit))
        {
            $query .= ' LIMIT ' . $limitBegin . ', ' . $limit;
        }

        $result = $this->_sendQuery($query);

        $resultArray = array();

        while ($row = mysqli_fetch_assoc($result)) 
        {
            $resultArray[] = $row;
        }

        if ($monitoring) 
        {       
            // If monitoring is activated, echo the query
            echo $query;
        }
        return $resultArray;
    }    

I want to use this line "while($row = mysqli_fetch_assoc($result))" Please advice!

Barbara Laird
  • 12,599
  • 2
  • 44
  • 57
Hassan
  • 303
  • 2
  • 8
  • 25

3 Answers3

5

Your select method is returning an array, not a resource. This means that mysqli_fetch_assoc is right to complain.

The good news is that the select method is returning an array of results, which means you can replace while($row = mysqli_fetch_assoc($result)) with

foreach($result as $row)
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

That select() method clearly performs the query and then returns an array of results. Why would you expect to be able to (or even need to) take that array and do a mysqli_fetch_assoc() on it again?

I think you can just iterate directly over $result like this:

$modelmake = $_POST['model_make'];
$result = $db->select('car_information','*', 'Model_Make LIKE \'%$modelmake%\''); 
foreach ($result as $row)
    echo 'Model'.$row['model_make'];
}

It should also be pointed out that your code is vulnerable to SQL injection.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
-1

Use mysqli_error() http://www.php.net/manual/en/mysqli.errno.php

Returns the text of the error message from previous MySQL operation

mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given

Run your query from the MySQL command line or a tool like phpMyAdmin. If you have a syntax error in your query this will tell you what it is.

Make sure your quotes are correct. A missing quote around the query or a value can cause a query to fail.

Make sure you are escaping your values. Quotes in your query can cause a query to fail (and also leave you open to SQL injections). Use mysql_real_escape_string() to escape your input.

Community
  • 1
  • 1
zloctb
  • 10,592
  • 8
  • 70
  • 89
  • var_dump($result) returns array(0) { } "mario" than how to tackle this situation please correct I am a newbie to oop programing – Hassan Jul 23 '13 at 20:56