关于mysql:PHP数据库连接类

PHP database connection class

我试图从一个类中的数据库获取用户ID,但是我对类的经验很少甚至没有,如何从数据库中获取uid然后返回uid?

基本上就是这样,

1
2
3
4
5
6
7
8
9
10
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;
   }
}

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

预先感谢!


首先构建一个MySQL类库...以适合此样例片段中的要求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?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);
    }
}
?>

和配置文件...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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';
    }
}
?>


好的,一条建议:

做所有事情都是有原因的。不要使用您不知道的东西。去学习吧。

一个特定的问题可能会给您答案,但是直到您不了解面向对象的含义以及根本没有类的原因,才不应使用它们。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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;
    }
}
?>

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

1
$array = mysql_fetch_array($result);

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

可能的伪代码为:

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

首先查看相关文档:

  • http://php.net/manual/zh/function.mysql-connect.php
  • http://www.php.net/manual/zh/function.mysql-query.php
  • http://www.php.net/manual/zh/function.mysql-fetch-array.php

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

请尝试以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.." . 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("");