gpt4 book ai didi

php - php中查找intent连接函数

转载 作者:行者123 更新时间:2023-11-29 10:32:53 24 4
gpt4 key购买 nike

这是用于连接 MYSQL 的 db.php 文件。我正在使用1.单个实例的self在所有实例之间共享2.db连接配置变量3.此方法必须是静态的,并且如果对象不存在,则必须返回该对象的实例。4.clone和wakeup方法防止外部实例化Singleton类的副本,从而消除了重复对象的可能性。

<?php
include('config.php');
class db extends mysqli {


// single instance of self shared among all instances
private static $instance = null;


// db connection config vars
private $user = DBUSER;
private $pass = DBPWD;
private $dbName = DBNAME;
private $dbHost = DBHOST;

//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}

// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}

private function __construct() {
parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
if (mysqli_connect_error()) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
parent::set_charset('utf-8');

}
public function dbquery($query)
{
if($this->query($query))
{
return true;
}

}
public function get_result($query)
{
$result = $this->query($query);
if ($result->num_rows > 0){
while ($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
} else
return null;


}
}


?>

这是插入带有 Escape 用户输入的绳索以确保安全

<?php

$Institute_Name = mysqli_real_escape_string($conn, $_REQUEST['Institute_Name']);
$Institute_Address = mysqli_real_escape_string($conn, $_REQUEST['Institute_Address']);

?>

我需要如何从实例调用$conn

$conn = getInstance();

我试试这个。这不起作用我需要它来插入代码

$query= "INSERT INTO `institute` ( Institute_Name, Institute_Address, ) VALUES ('$Institute_Name', '$Institute_Address')";

最佳答案

你应该使用

$conn = db::getInstance();

关于php - php中查找intent连接函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47068982/

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