gpt4 book ai didi

php - 尝试检索结果时,使用 PHP 类连接到数据库不起作用

转载 作者:行者123 更新时间:2023-11-28 23:44:34 26 4
gpt4 key购买 nike

早上好。

我正在尝试创建一个数据库类来连接我的数据库并从中检索结果。所以,我觉得跟类就是一切都好。但是,结果不会出现。

这是我的数据库类:

<?php class Conexao {

var $host = "localhost";
var $usuario = "root";
var $senha = "xxxxxx";
var $banco = 'restaurante';
private $mysqli;

public function Abrir()
{
$this->mysqli = new mysqli($this->host, $this->usuario, $this->senha, $this->banco);
}

public function Fechar()
{
$this->mysqli->close();
}
}

class Comando {
public function Executar($sql)
{
$con = new Conexao();
$con->Abrir();
$re = $con->mysqli->query($sql);
$con->Fechar();
return $re;
}
}
?>

这是我尝试检索结果的地方:

<?php

$queryMesasAtivas = Comando::Executar('SELECT * FROM mesas WHERE status =1 AND numero !="'.$_SESSION["mesa"].'"');

if ($queryMesasAtivas->num_rows > 0) {

while ($rowMesasAtivas = $queryMesasAtivas->fetch_assoc()) {
echo "<option value='".$rowMesasAtivas['numero']."'>Mesa ".$rowMesasAtivas['numero']."</option>";
}
}
else {
echo '<option>Nenhuma mesa ativa</option>';
}

?>

我尝试了一些修改,但没有任何改变。每次还是不行。怎么了?

最佳答案

由于 Comando::Executar 不是静态的,而是声明为 public function...,因此您必须执行以下操作:

$comando = new Comando();

$queryMesasAtivas = $comando->Executar('SELECT * FROM mesas WHERE status =1 AND numero !="'.$_SESSION["mesa"].'"');

if ($queryMesasAtivas->num_rows > 0) {

while ($rowMesasAtivas = $queryMesasAtivas->fetch_assoc()) {
echo "<option value='".$rowMesasAtivas['numero']."'>Mesa ".$rowMesasAtivas['numero']."</option>";
}
}
else {
echo '<option>Nenhuma mesa ativa</option>';
}

或者将方法声明为static,即:

public static function Executar($sql)
{
$con = new Conexao();
$con->Abrir();
$re = $con->mysqli->query($sql);
$con->Fechar();
return $re;
}

然后你可以使用双冒号(::)语法:

$queryMesasAtivas = Comando::Executar('SELECT * FROM mesas WHERE status =1 AND numero !="'.$_SESSION["mesa"].'"');

我建议不要每次运行查询时都调用打开和关闭,而是像这样的类:

class Conexao
{
private $link;

public function __construct($host = null, $username = null, $password = null, $dbName = null)
{
$this->link = mysqli_init();
$this->link->real_connect($host, $username, $password, $dbName) or die("Failed to connect");
}

public function __destruct()
{
$this->link->close();
}

public function Query($sql)
{
return $this->link->query($sql);
}
}

然后这样使用:

$conexao = new Conexao("host", "username", "password", "db_name");
$result = $conexao->Query("SELECT * FROM `table` WHERE 1 ORDER BY `id` ASC;");

这不仅更小,而且在服务器上更轻量级,因为您不会永久打开和关闭数据库连接,从而减少 CPU 使用和内存使用。

为主机等使用静态属性(即使在使用 __destruct 之后,它们也会保留在内存中,因此您不需要每次都重新声明它们):

class Conexao
{
private $link;
private static $host, $username, $password, $dbName;

public function __construct($host = null, $username = null, $password = null, $dbName = null)
{
static::$host = $host ? $host : static::$host;
static::$username = $username ? $username : static::$username;
static::$password = $password ? $password : sattic::$password;
static::$dbName = $dbName : $dbName : static::$dbName;
$this->link = mysqli_init();
$this->link->real_connect(static::$host, static::$username, static::$password, static::$dbName) or die("Failed to connect");
}

public function __destruct()
{
$this->link->close();
}

public function Query($sql)
{
return $this->link->query($sql);
}
}

$conexao = new Conexao("host", "username", "password", "db_name");
$result = $conexao->Query("SELECT * FROM `table` WHERE 1 ORDER BY `id` ASC;");

$conexao->__destruct(); // Destroy the class
$conexao = new Conexao(); // Reinitialise it
$result = $conexao->Query("SELECT * FROM `table` WHERE 1 ORDER BY `id` ASC;");

使用连接类的配置实例:

config.php 文件:

<?php

require_once 'path/to/Conexao.php';
$conexao = new Conexao("host", "username", "password", "db_name");

?>

index.php 文件:

<?php

require_once 'config.php';
$result = $conexao->Query("SELECT * FROM `table` WHERE 1 ORDER BY `id` ASC;");

?>

该类现在在 my github 上有一个父类!

关于php - 尝试检索结果时,使用 PHP 类连接到数据库不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825065/

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