isuruAb
isuruAb

Reputation: 2240

Getting Fatal error: Call to a member function query() on a non object error while including database details twice

I have 3 files in my program those are database.php ,index.php,functions.php When I click next button of dashboard.php file it shows these two errors.

Notice: Undefined variable: conn in C:\wamp\www\quiz\functions.php on line 144

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\quiz\functions.php on line 41

Line number 144 is $result = $conn->query($sql); when I replace include_once('database.php');line of getQuizes_answer1() function with the content of the database.php file it works fine. Is there anyone who can clarify why this is happening like this?

this is how my functions.php file looks like

function getQuizes($quizNo)
{
    if($quizNo==NULL)
    {
        $quizNo=0;
    }
    require('database.php');
    $sql = "SELECT * FROM quiz LIMIT ".$quizNo.",1";
    $result = $conn->query($sql);
    while($row=$result->fetch_assoc())
    {
        echo $row['question'];
    }
    $quizNo++;
    $sql = "SELECT * FROM quiz";
    $result = $conn->query($sql);
    $rowcount=mysqli_num_rows($result);

    if($quizNo>=$rowcount)
    {
        $quizNo=0;
    }
    return $quizNo;
}

function getQuizes_answer1($quizNo)
{
    include_once('database.php');
    if($quizNo!=0)
    {
        $quizNo--;
    }
    $sql = "SELECT * FROM quiz LIMIT ".$quizNo.",1";
    $result = $conn->query($sql);
    while($row=$result->fetch_assoc())
    {
        echo $row['answer1'];
    }
    $sql = "SELECT * FROM quiz";
    $result = $conn->query($sql);
    $rowcount=mysqli_num_rows($result);
    if($quizNo>=$rowcount)
    {
        $quizNo=0;
    }
$conn->close();
}

This is how my dashboard.php file looks like

<?php
    $i = isset($_SESSION['next']) ? $_SESSION['next'] : NULL;
    if(isset($_POST['next']))
    {
        $i = getQuizes($i);
        $_SESSION['next'] = $i;
 ?>
 <div class="answers_block">
     <table>
                <tr>
                    <td><?php getQuizes_answer1($i);?></td>
                </tr>
     </table>
 </div>
     <?php
     }
     ?>
       <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           <input name="next" value="Next" type="submit" />
       </form>

my database.php file looks like this

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database="quiz_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

Upvotes: 1

Views: 1167

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The problem is, your connection handler $conn is not available in the scope of your function. Either use global or pass the connection handler as an argument to your function.

Method(1):

function getQuizes($quizNo){
    global $conn;

    // your code
}

function getQuizes_answer1($quizNo){
    global $conn;

    // your code
}

Method(2):

function getQuizes($conn, $quizNo){

    // your code

}

function getQuizes_answer1($conn, $quizNo){

    // your code

}

Upvotes: 2

Related Questions