I have a website in core PHP and I get error like this

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given

Code

$query = "SELECT * FROM questions WHERE id='question' LIMIT 5";
$result = mysqli_query($connection, $query);
if($query === FALSE) { die(mysql_error()); } 

while($row = mysqli_fetch_array($query)){
    $id = $row['id'];
    $thisQuestion = $row['question'];
    $question_id = $row['question_id'];
    $q = '<h2>'.$thisQuestion.'</h2>';
    $query2 = "SELECT * FROM answers WHERE question_id='$question' ORDER BY rand() LIMIT 5";
    while($row2 = mysqli_fetch_array($query2)){
        //...
    }
}

The problem I'm having is from the 4th line of code listed below. I get an error that says

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given.

php mysql 

Nov 17

4 Answers

You have:

 mysqli_fetch_array($query)

That should be:

mysqli_fetch_array($result) 

Also in line 3 you have:

if($query === FALSE) { die(mysql_error()); } 

Should be rather:

if ($result === FALSE) { die(mysql_error()); } 

answered Dec 02


mysqli_fetch_array()'s 1st parameter must be a result of a query. What you are doing is you are passing the connection (which doesn't make sense) and the query command itself.

To fix this, execute the query first, then store the result to a variable then later fetch that variable.

$sql = "select * from privinsi";
$result = mysqli_query($connection,$sql);
while($r = mysqli_fetch_array($result))
{
    // your code here
}

answered Dec 13


mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in code example

Example 1: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string is given in

mysqli_fetch_array($result)

Example 2: mysqli_fetch_array() expects parameter 1 to be mysqli_result

$sql = "select * from privinsi";
$result = mysqli_query($connection,$sql);
while($r = mysqli_fetch_array($result))
{
    // your code here
}

Example 3: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in

mysqli_fetch_array($query)

answered Dec 30


I wouldn't try to return anything inside the constructor. Why not call a separate function to get the connection?

class Database {

    function __construct() {}
    function getConnection(){
        $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
        return $connection;
    }
}

and then get the connection from the Database object after instantiation:

$result = mysqli_query($db->getConnection(),"SELECT * from members WHERE email = '$emailusername' or username='$emailusername' and password = '$password'") or die(mysqli_error($db));

answered Dec 30


Login and Submit Your Answer
Browse other questions tagged  php  mysql or ask your own question.