Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数据库连接类_Php_Mysql_Class - Fatal编程技术网

PHP数据库连接类

PHP数据库连接类,php,mysql,class,Php,Mysql,Class,我试图从类中的数据库中获取用户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

我试图从类中的数据库中获取用户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;
   }
}
就像我说的,我还是新手,所以任何建议或帮助都将不胜感激

先走一步

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

    • 好的,有一条建议:

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


      有人可能会给你这个特定问题的答案,但除非你不知道是什么意思以及为什么会有,否则你不应该使用它们。

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

      $array = mysql_fetch_array($result);
      
      这是变量
      $result
      第一次出现在函数上。因此,无法与数据库通信

      可能的伪代码是:

      • 连接到数据库服务器
      • 查询数据库
      • 取得结果
      • 返回
        uid
        字段
      首先查看相关文档:


      首先构建一个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);
          }
      }
      ?>
      
      尝试以下操作:

      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("");
      
      ini_集(“显示错误”,“关闭”);
      ini_集合(“错误报告”,E_全部);
      类myclass{
      函数myclass(){
      $user=“root”;
      $pass=“”;
      $server=“localhost”;
      $dbase=“”;
      $conn=mysql\u connect($server、$user、$pass);
      如果(!$conn)
      {
      $this->错误(“连接尝试失败”);
      }
      如果(!mysql\u select\u db($dbase,$conn))
      {
      $this->错误(“数据库选择失败”);
      }
      $this->CONN=$CONN;
      返回true;
      }
      函数close(){
      $conn=$this->conn;
      $close=mysql\u close($conn);
      如果(!$close)
      {
      $this->错误(“连接关闭失败”);
      }
      返回true;
      }函数sql\u query($sql=”“){
      if(空($sql))
      {
      返回false;
      }
      if(空($this->CONN))
      {
      返回false;
      }
      $conn=$this->conn;
      $results=mysql\u query($sql,$conn)或die(“查询失败..
      ”.mysql\u error()); 如果(!$results) { $message=“错误查询!”; $this->error($message); 返回false; } 如果(!(eregi(^select,$sql)| eregi(^show,$sql))) { 返回true; } 其他的 { $count=0; $data=array(); while($row=mysql\u fetch\u数组($results)) { $data[$count]=$row; $count++; } mysql_免费_结果($results); 返回$data; } } } $obj=新的myclass(); $obj->sql\U查询(“”);
      您可以使用自定义数据库类。
      代码:

      <?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;
          }
      }
      ?>
      
      
      
      Thanx获得建议!目前我正在边走边学习,这就是为什么我需要一些帮助。仅供参考:当您使用此代码时,请确保您正在验证查询中的数据,因为这将允许SQL注入。尽管如此,我还是喜欢这种方法,即使多年以后:)
      <?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;
          }
      }
      ?>