dsmgcse8876 2016-05-10 05:36
浏览 3
已采纳

too long

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:\websitesdpa\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?

  • 写回答

2条回答 默认 最新

  • duanfen9983 2016-05-10 06:44
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改