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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100