Frank G. Frank G. - 7 months ago 36
SQL Question

Call to a member function query() on a non-object in

I am trying to get php to simplify my database connections so that I can modify one area of the code and effect the entire program. Here is what I have:

PAGE: rdpa.php (A modules page

/**************************/
/* Create System Settings */
/**************************/

/* Database Connection Settings */
$_SESSION['servername'] = "localhost";
$_SESSION['mysql_username'] = "username";
$_SESSION['mysql_password'] = "password";
$_SESSION['dbname'] = "mydb";

//Turn on Error Report. True = On / False = Off
//ErrorReporting(false);

//Display Error.kfkg
function ErrorReporting($ErrOn){
if ($ErrOn == true) {
//Show Error
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
}
}

/**************************************
Open Database Connection Function.
***************************************/

class db_class {

function db_conn() {

global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}

//return db_conn;
}

}


PAGE: testdb3.php

<?php

//Included file.
include 'modules/rdpa.php';

error_reporting(-1);
ini_set('display_errors', 'On');

//Access our class.
$db_Class = new db_Class;
$conn = ($db_Class->db_conn());

//connect to the database.
$sql = "SELECT id, region FROM tbl_region;";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {

echo '<td width="1"><label for="delete">
<input type="radio" name="region" id="region" value="'.$row["id"].'">
</label></td>';

echo '<td align="left" valign="top"><p>'.$row["region"].'</p></td></tr>';
}
}


?>


When I run testdb3.php I get the following error:

Fatal error: Call to a member function query() on a non-object in C:\websites\rdpa\testdb3.php on line 15

This is Line 15:

//connect to the database.
$result = $conn->query($sql);


Can some please tell me what I am doing wrong here?

Apb Apb
Answer

Change your rdpa.php page to

class db_class {

    function db_conn() {

        global $conn;
        global $mysqli;
        $conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);

            // Test if connection succeeded
            if (mysqli_connect_errno()) {
                die("Database connection failed: " .
                                mysqli_connect_error() .
                                " (" . mysqli_connect_errno() . ")"
                );
            }
        }
        return $conn;       //return connection string
    }

}           //this is missing in your code

You need to return connection string from function. Right now you are returning $conn in if loop i.e. if error occurred then return connection this is what you are doing.

Please use some IDE to code. } for class is missing in your code