0

Got this type of errors:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\OpenServer\domains\Test.loc\PHP\Shop Cart\index.php on line 7

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\OpenServer\domains\Test.loc\PHP\Shop Cart\index.php on line 8

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\OpenServer\domains\Test.loc\PHP\Shop Cart\index.php on line 11

//Config File
<?php
    session_start();
    $connect = mysqli_connect ('localhost', 'test', 'password')
    or die(mysqli_error());
    mysqli_select_db($connect, 'training');

?>




<?php

    include_once('config.php');


function findProducts() {
    $query = mysqli_query ($connect, "SELECT * FROM cart");
    $result = mysqli_fetch_array($query);


    while($row = mysqli_fetch_array( $result)){
        echo $row['title'].'<br>';
    }
}


findProducts();
?>
2
Contributors
1
Reply
14
Views
2 Years
Discussion Span
Last Post by cereal
0

It happens because $connect is defined out of the scope of the function, to fix it do:

<?php

include 'config.php';

function findProducts($connect) {
    $query = mysqli_query($connect, "SELECT * FROM cart");
    while($row = mysqli_fetch_assoc($query)){
        echo $row['title'].'<br>';
    }
}

echo findProducts($connect)

Edited by cereal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.