gpt4 book ai didi

php - fatal error : Call to a member function query() PHP CLASS

转载 作者:行者123 更新时间:2023-11-29 04:40:15 25 4
gpt4 key购买 nike

我正在尝试编写此类来连接和查询我的数据库,但出现此错误:

Fatal error: Call to a member function query() on null in C:\xxxxxx\xxxx\xxxxx\xxxxxxx\pages\config\class.php on line 24

PHP代码:

<?php

class Db{

private static $db_host = "localhost";
private static $db_user = "root";
private static $db_pass = "";
private static $db_name = "sivi";

public $connection;

public function db_connect() {

$connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection));
echo "Conexión realizada". "<br>";

}

public function db_query($query){

$connection = $this->db_connect();
var_dump($query);
$result = $connection->query($query);
while($row = mysqli_fetch_array($result)) {
echo $row["COD_PRE"] . "<br>";
}

}

}

$con = new Db();
$con->db_query('SELECT `COD_PRE`, `CODE` FROM `test` WHERE `CODE` = 457 AND CONFIN = 1');

?>

最佳答案

你的方法:

public function db_connect() {    

$connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection));
echo "Conexión realizada". "<br>";

}

不会从方法中重新调整 $connection,因此其余方法调用失败,请执行此操作:

public function db_connect() {    

$connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection));
echo "Conexión realizada". "<br>";
return $connection; // return the $connection object
}

如前所述,您的代码效率不高,因为对于执行的每个查询,代码都会(重新)连接到数据库。这是不必要的昂贵/低效。

有很多方法可以解决这个问题。

  1. 在数据库类实例化时连接到数据库

例如

class Db{

private static $db_host = "localhost";
private static $db_user = "root";
private static $db_pass = "";
private static $db_name = "sivi";

public $connection;

public function __construct()
{
$this->connection = $this->db_connect();
}

public function db_connect() {

$connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection));
echo "Conexión realizada". "<br>";
return $connection;
}
}
  1. 惰性连接,即只在第一次执行查询时连接

例如

class Db{

private static $db_host = "localhost";
private static $db_user = "root";
private static $db_pass = "";
private static $db_name = "sivi";
public $connection = null;
public function __construct()
{
}
public function db_connect() {
$connection = mysqli_connect(self::$db_host, self::$db_user, self::$db_pass, self::$db_name) or die("Error " . mysqli_error($connection));
echo "Conexión realizada". "<br>";
return $connection;
}
public function db_query($query){
if ( null ==== $this->connection ) $this->connection = $this->db_connect();
var_dump($query);
$result = $this->connection->query($query);
while($row = mysqli_fetch_array($result)) {
echo $row["COD_PRE"] . "<br>";
}
}
}

关于php - fatal error : Call to a member function query() PHP CLASS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30628563/

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