PHP数据库连接类

时间:2010-07-12 12:57:07

标签: 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;
   }
}

就像我说的那样,我还是新上课,所以任何建议或帮助都会非常感激!

提前Thanx!

6 个答案:

答案 0 :(得分:33)

首先构建一个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';
    }
}
?>

答案 1 :(得分:8)

好的,有一条建议:

出于某种原因做所有事情。不要使用你不知道的东西。转而学习它们。

有人可能会就这个具体问题给出答案,但直到您不知道 Object Oriented 的含义以及为什么 classes 根本不应该使用它们。

答案 2 :(得分:4)

  • 创建两个类。一个用于处理数据库,另一个用于管理User或Auth数据。
  • 对于SQL类,创建方法connect(),query(),fetch()等
  • 对于User类,创建方法get($ id)etc

答案 3 :(得分:4)

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

$array = mysql_fetch_array($result);

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

可能的伪代码是:

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

首先查看相关文档:

答案 4 :(得分:3)

请尝试以下操作:

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("");

答案 5 :(得分: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;
    }
}
?>