drwg89980 2016-02-18 07:59
浏览 115
已采纳

通过类访问dbconfig文件 - PHP

I would like to insert data to the database for which I have a class called InsertTodb. But I am unable to access $dbc variable (which is in dbconfig .php) through class. Here is the code

My Class File

<?php
require("dbconfig.php");
class InsertTodb {
    public $tableNme;
    public $data1;
    public $data2;
    public $data3;
    public $arg1;
    public $arg2;
    public $arg3;


    function insertData($tableName, $data1, $data2, $data3, $val1, $val2, $val3) {
        $this->tableNme = $tableName;       
        $this->data1 = $data1;
        $this->data2 = $data2;
        $this->data3 = $data3;
        $this->arg1 = $val1;        
        $this->arg2 = $val2;        
        $this->arg3 = $val3;        

        $insquery = "insert into ".$this->tableNme."(".$this->data1.", ".$this->data2.", ".$this->data3.") values('".$this->arg1."', '".$this->arg2."',' ".$this->arg3."')";
        echo $insquery; 

        if(mysqli_query($dbc, $insquery)) {                 
        $success = "Product added successfully."; 
        echo $success; 
        }
        else {
        $failed = "Error adding product."; 
        echo $failed; 
        }
}
}
?>

My dbconfig file

<?php
$db_hostname = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'oop';

$dbc1 = mysqli_connect ($db_hostname,$db_username, $db_password,$db_name);

if (mysqli_connect_errno()) {
echo "Could not establish database connection!";
exit();
}
?>

My code

<?php

include "InsertTOdb.php";

$ins = new InsertTodb();

$ins->insertData("tableone", "name", "age", "desig",  "Guru", "25", "Accountant");

?>

When I run the above program, it shows the error "Notice: Undefined variable: dbc" and ..." Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ...". I am new to OOP. Please help to fix it.

  • 写回答

2条回答 默认 最新

  • dongmiao4733 2016-02-18 08:08
    关注

    You should refactor your code a little. First of all,

    <?php
    include "dbconfig.php"; // Add this first.
    include "InsertTOdb.php";
    $ins = new InsertTodb($dbc1); // Feed the connector to your class.
    $ins->insertData("tableone", "name", "age", "desig",  "Guru", "25", "Accountant");
    ?>
    

    Now change the InsertTOdb class a little:

    class InsertTodb {
        private $dbc;
        public function __construct($dbc) {
            $this->dbc = $dbc;
        }
    
        // ... the rest of class's properties ...
        function insertData($tableName, $data1, $data2, $data3, $val1, $val2, $val3) {
            ...
            // Change $dbc to $this->dbc.
            if(mysqli_query($this->dbc, $insquery)) {
                ...
            }
            ...
        }
        ...
    }
    

    Your insertData() looks a bit clunky with all these $data1, $data2 etc. values (you should pass them all at once as arrays or objects), but this should be enough for now.

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

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助