gpt4 book ai didi

PHP 和 MySQL OOP - 为什么我没有得到返回结果?

转载 作者:行者123 更新时间:2023-11-30 22:25:50 26 4
gpt4 key购买 nike

我不确定为什么没有在浏览器中返回/输出全名。谁能帮忙?我有一个包含特定类的类文件,只是为了更有条理的文件结构。数据库类包含所有当前与数据库有关的方法,用户类包含用户的方法,我在用户类中调用数据库中的一些方法,但我确定方法和属性的范围是设置正确吗?

类.php:

include 'database.php';
include 'user.php';
$user = User::getUserId(1);
echo $user->getName();

数据库.php

<?php
require_once("../core/config.php");

class Database {
private $connection;

function __construct() {
$this->connect();
}

public function connect() {
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
}

public function disconnect() {
if(isset($this->connection)) {
mysqli_close($this->connection);
unset($this->connection);
}
}

public function query($sql) {
$result = mysqli_query($this->connection, $sql);
if (!$result) {
die("Database query failed.");
}
return $result;
}

public function prepare($data) {
$escString = mysqli_real_escape_string($this->connection, $data);
return $escString;
}

public function fetchArray($results) {
return mysqli_fetch_array($results);
}
}

$db = new Database();

?>

用户.php

<?php

class User {

public $userId;
public $username;
public $password;
public $email;
public $firstname;
public $lastname;
public $access;
public $active;

public static function getUsers() {
return self::getBySQL("SELECT * FROM users");
}

public static function getUserId($id=0) {
global $db;
$result = self::getBySQL("SELECT * FROM users WHERE userId={$id} LIMIT 1");
return !empty($result) ? array_shift($result) : false;
}

public static function getBySQL($sql) {
global $db;
$result = $db->query($sql);
$objArray = array();
while ($row = $db->fetchArray($result)) {
$objArray[] = self::instantiate($row);
}
return $objArray;
}

public function getName() {
if (isset($this->firstname) && isset($this->lastname)) {
return $this->firstname . " " . $this->lastname;
} else {
return "";
}
}

private static function instantiate($record) {
$object = new self;

foreach($record as $attr=>$value){
if($object->hasAttr($attr)) {
$object->$attr = $value;
}
}
return $object;
}

private function hasAttr($attr) {
$objectVars = get_object_vars($this);
return array_key_exists($attr, $objectVars);
}
}

?>

最佳答案

好吧,您的代码有效。这是一个独立的示例:

<?php
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "sotest_users");

function query($mysqli, $text) {
print 'Query: ' . $text . PHP_EOL;
$result = $mysqli->query($text);
if ($result === false) {
print 'Query error: ' . $mysqli->error . PHP_EOL;
die();
}
return $result;
}

$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS);
query($mysqli, 'drop database if exists ' . DB_NAME);
query($mysqli, 'create database ' . DB_NAME);
$mysqli->select_db(DB_NAME);
query($mysqli, 'create table users (userId integer, username text, password text)');
query($mysqli, 'insert into users (userId, username, password) values (1, "test", "test")');

$result = query($mysqli, 'select * from users where userId=1');
while ($row = $result->fetch_assoc()) {
print json_encode($row) . PHP_EOL;
}
$result->close();


// Your code here

class Database {
//...
}

$db = new Database();

class User {
//...
}

print 'Start' . PHP_EOL;
$user = User::getUserId(1);
var_dump($user);
//echo $user->getName();

输出:

Query: drop database if exists sotest_users
Query: create database sotest_users
Query: create table users (userId integer, username text, password text)
Query: insert into users (userId, username, password) values (1, "test", "test")
Query: select * from users where userId=1
{"userId":"1","username":"test","password":"test"}
Start
class User#6 (8) {
public $userId =>
string(1) "1"
public $username =>
string(4) "test"
public $password =>
string(4) "test"
public $email =>
NULL
public $firstname =>
NULL
public $lastname =>
NULL
public $access =>
NULL
public $active =>
NULL
}

完整代码是 here .password 之后的字段为空,只是因为我没有将它们添加到 users 表中。

但我必须说,无论是 OOP(全局变量、静态方法、公共(public)属性等)还是数据库处理(缺乏错误处理、直接变量注入(inject)进入 sql)。

如果这是一个真实的项目 - 最好切换到一些框架,比如 Yii .

如果您正在学习 - 查找并阅读有关 php 中正确的 OOP 用法和最佳数据库实践的内容。 PHP The Right Way是一个很好的起点。

关于PHP 和 MySQL OOP - 为什么我没有得到返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35257288/

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