PHP database connection class

后端 未结 6 1974
名媛妹妹
名媛妹妹 2020-12-08 05:29

I am trying to get a users ID from a database within a class, but I have very little to no experience with classes, how could I go about getting the uid from the DB and then

6条回答
  •  Happy的楠姐
    2020-12-08 06:24

    First build a MySQL class library... suiting the requirements like in this sample piece:

     connectionString = NULL;
        $this -> sqlQuery = NULL;
        $this -> dataSet = NULL;
    
                $dbPara = new Dbconfig();
                $this -> databaseName = $dbPara -> dbName;
                $this -> hostName = $dbPara -> serverName;
                $this -> userName = $dbPara -> userName;
                $this -> passCode = $dbPara ->passCode;
                $dbPara = NULL;
    }
    
    function dbConnect()    {
        $this -> connectionString = mysql_connect($this -> serverName,$this -> userName,$this -> passCode);
        mysql_select_db($this -> databaseName,$this -> connectionString);
        return $this -> connectionString;
    }
    
    function dbDisconnect() {
        $this -> connectionString = NULL;
        $this -> sqlQuery = NULL;
        $this -> dataSet = NULL;
                $this -> databaseName = NULL;
                $this -> hostName = NULL;
                $this -> userName = NULL;
                $this -> passCode = NULL;
    }
    
    function selectAll($tableName)  {
        $this -> sqlQuery = 'SELECT * FROM '.$this -> databaseName.'.'.$tableName;
        $this -> dataSet = mysql_query($this -> sqlQuery,$this -> connectionString);
                return $this -> dataSet;
    }
    
    function selectWhere($tableName,$rowName,$operator,$value,$valueType)   {
        $this -> sqlQuery = 'SELECT * FROM '.$tableName.' WHERE '.$rowName.' '.$operator.' ';
        if($valueType == 'int') {
            $this -> sqlQuery .= $value;
        }
        else if($valueType == 'char')   {
            $this -> sqlQuery .= "'".$value."'";
        }
        $this -> dataSet = mysql_query($this -> sqlQuery,$this -> connectionString);
        $this -> sqlQuery = NULL;
        return $this -> dataSet;
        #return $this -> sqlQuery;
    }
    
    function insertInto($tableName,$values) {
        $i = NULL;
    
        $this -> sqlQuery = 'INSERT INTO '.$tableName.' VALUES (';
        $i = 0;
        while($values[$i]["val"] != NULL && $values[$i]["type"] != NULL)    {
            if($values[$i]["type"] == "char")   {
                $this -> sqlQuery .= "'";
                $this -> sqlQuery .= $values[$i]["val"];
                $this -> sqlQuery .= "'";
            }
            else if($values[$i]["type"] == 'int')   {
                $this -> sqlQuery .= $values[$i]["val"];
            }
            $i++;
            if($values[$i]["val"] != NULL)  {
                $this -> sqlQuery .= ',';
            }
        }
        $this -> sqlQuery .= ')';
                #echo $this -> sqlQuery;
        mysql_query($this -> sqlQuery,$this ->connectionString);
                return $this -> sqlQuery;
        #$this -> sqlQuery = NULL;
    }
    
    function selectFreeRun($query)  {
        $this -> dataSet = mysql_query($query,$this -> connectionString);
        return $this -> dataSet;
    }
    
    function freeRun($query)    {
        return mysql_query($query,$this -> connectionString);
      }
    }
    ?>
    

    and the configuration file...

     serverName = 'localhost';
            $this -> userName = 'root';
            $this -> passCode = 'pass';
            $this -> dbName = 'dbase';
        }
    }
    ?>
    

提交回复
热议问题