【问题标题】:PHP database connection classPHP数据库连接类
【发布时间】:2011-03-14 19:25:47
【问题描述】:

我正在尝试从类中的数据库中获取用户 ID,但我几乎没有使用类的经验,我该如何从数据库中获取 uid 然后返回 uid?

所以基本上是这样的,

class hello {
   public function getUid(){
      //connect to the db
      //get all of the users info
      $array = mysql_fetch_array($result);
      $uid = $array['uid'];

      return $uid;
   }
}

就像我说的,我还是新手,所以任何建议或帮助都将不胜感激!

提前谢谢!

【问题讨论】:

    标签: php mysql class


    【解决方案1】:

    首先构建一个 MySQL 类库...适合此示例中的要求:

    <?php
    
    include '../config/Dbconfig.php';
    
    class Mysql extends Dbconfig {
    
        public $connectionString;
        public $dataSet;
        private $sqlQuery;
        
        protected $databaseName;
        protected $hostName;
        protected $userName;
        protected $passCode;
    
        function Mysql() {
            $this -> 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);
        }
    }
    ?>
    

    还有配置文件...

    <?php
    class Dbconfig {
        protected $serverName;
        protected $userName;
        protected $passCode;
        protected $dbName;
    
        function Dbconfig() {
            $this -> serverName = 'localhost';
            $this -> userName = 'root';
            $this -> passCode = 'pass';
            $this -> dbName = 'dbase';
        }
    }
    ?>
    

    【讨论】:

    • 仅供参考:当您使用此代码时,请确保您正在验证来自查询的数据,因为这将允许 SQL 注入。尽管如此,即使在多年之后,我仍然喜欢这种方法:)
    【解决方案2】:

    好的,一条建议:

    做任何事都是有原因的。不要使用你不知道的东西。而是去学习它们。

    对于这个特定问题,有人可能会给你一个答案,但直到你不知道 Object Oriented 是什么意思以及为什么会有 classes 之前,你不应该使用它们。

    【讨论】:

    • 感谢您的建议!目前我正在边做边学,这就是为什么我需要一些帮助。
    【解决方案3】:

    您可以使用自定义数据库类。
    代码:

    <?php 
    Class Database
    {
        private $user ;
        private $host;
        private $pass ;
        private $db;
    
        public function __construct()
        {
            $this->user = "root";
            $this->host = "localhost";
            $this->pass = "";
            $this->db = "db_blog";
        }
        public function connect()
        {
            $link = mysql_connect($this->user, $this->host, $this->pass, $this->db);
            return $link;
        }
    }
    ?>
    

    【讨论】:

      【解决方案4】:
      • 创建两个类。一种用于处理数据库,另一种用于管理用户或身份验证数据。
      • 对于 SQL 类创建方法 connect()、query()、fetch() 等
      • 对于用户类创建方法 get($id) 等

      【讨论】:

        【解决方案5】:

        您的代码编写方式存在问题,而不是类。仔细看看这一行:

        $array = mysql_fetch_array($result);
        

        这是变量$result第一次出现在函数中。因此,无法与数据库进行通信。

        可能的伪代码是:

        • 连接数据库服务器
        • 查询数据库
        • 获取结果
        • 返回uid 字段。

        先看看相关文档:

        【讨论】:

          【解决方案6】:

          尝试以下操作:

          ini_set("display_errors", 'off');
              ini_set("error_reporting",E_ALL);   
          
          class myclass {
              function myclass()   {    
                  $user = "root";
                  $pass = "";
                  $server = "localhost";
                  $dbase = "";
          
                     $conn = mysql_connect($server,$user,$pass);
                     if(!$conn)
                  {
                      $this->error("Connection attempt failed");
                  }
                  if(!mysql_select_db($dbase,$conn))
                  {
                      $this->error("Dbase Select failed");
                  }
                  $this->CONN = $conn;
                  return true;
              }
              function close()   {   
                  $conn = $this->CONN ;
                  $close = mysql_close($conn);
                  if(!$close)
                  {
                      $this->error("Connection close failed");
                  }
                  return true;
              }       function sql_query($sql="")   {    
                  if(empty($sql))
                  {
                      return false;
                  }
                  if(empty($this->CONN))
                  {
                      return false;
                  }
                  $conn = $this->CONN;
                  $results = mysql_query($sql,$conn) or die("Query Failed..<hr>" . mysql_error());
                  if(!$results)
                  {   
                      $message = "Bad Query !";
                      $this->error($message);
                      return false;
                  }
                  if(!(eregi("^select",$sql) || eregi("^show",$sql)))
                  {
                      return true;
                  }
                  else
                  {
                      $count = 0;
                      $data = array();
                      while($row = mysql_fetch_array($results))
                      {
                          $data[$count] = $row;
                          $count++;
                      }
                      mysql_free_result($results);
                      return $data;
                   }
              }      
          } 
          $obj = new myclass();
          $obj->sql_query("");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-03
            • 1970-01-01
            • 1970-01-01
            • 2010-09-13
            • 2016-04-30
            • 1970-01-01
            • 2014-01-11
            • 2013-05-14
            相关资源
            最近更新 更多