gpt4 book ai didi

PHP数据库连接类

转载 作者:IT老高 更新时间:2023-10-29 00:17:48 24 4
gpt4 key购买 nike

我正在尝试从类中的数据库中获取用户 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;
}
}

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

提前谢谢!

最佳答案

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

关于PHP数据库连接类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3228694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com