我可以通过编辑程序PHP(只是)来完成,但是OOP是另一回事。所以我对我在这里所做的事情并不熟悉,但我正在努力......
我有一个名为Quote.object.php的文件,其中包含以下内容:
$Query = new DbQuery( "INSERT", "quotes", $array );
$this->id = mysqli_insert_id();
mysqli_insert_id需要提供数据库连接参数,但我不知道该怎么做。还有一个名为Mysql.handler.php的文件包含数据库连接变量 - 有没有一种方法可以将$ con作为$ Query的参数提供?
class DbQuery extends DbConnectionInfo{
// file: includes/classes/MysqlQuery.php
// contains functions needed to perform queries on mysql database and functions for necessary data processing for application
// SELECT = new DbQuery("select", table,cols[$value] ,where[$col=$value],order[$value],limit);
// INSERT = new DbQuery("insert", table, data[$col=$value]);
// UPDATE = new DbQuery("update", table, data[$col=$value],where[$col=$value],limit);
// set testing as true for SQL reports in page
var $results;
var $sql;
function __construct($mode,$table = '',$var1 = '',$var2 = '',$var3 = '',$var4='')
//connects to database according to info in DbConnectInfo, runs query, closes connection
{
$temp = '';
$con = mysqli_connect($this->host, $this->user, $this->pass) or die ('There was a problem connecting to the database ' . (ENVIRONMENT == 'Development' ? mysqli_error() . "$this->user, $this->pass, $this->host" : ''));
mysqli_select_db($con,$this->db) or die ('There was a problem connecting to the database' . (ENVIRONMENT == 'Development' ? mysqli_error() : ''));
我正在尝试从DbQuery获取$ con,因此我可以将其放入mysqli_insert_id()
。我认为这就是我需要的东西?有没有办法从DbQuery获得$ con并放入mysqli_insert_id()
?或者您需要更多信息才能知道这一点吗?
NB 我试图简明扼要地展示相关信息,如果我错过了其他有用的信息,我会道歉。
答案 0 :(得分:0)
您正在定义一个对象,因此请将$ con作为类变量,例如
function __construct() {
$this->con = mysqli_connect(...);
^^^^^^^----store in object
}
function foo() {
$result = mysqli_query($sql, $this->con);
^^^^^^^^---retrieve from object
}