gpt4 book ai didi

php - 将 php 参数绑定(bind)到类中的 mysqli 准备语句中

转载 作者:行者123 更新时间:2023-11-29 00:21:43 25 4
gpt4 key购买 nike

我正在考虑在我的 mysqli 查询中使用数据库表前缀。我有:

$db_table_prefix = "";
GLOBAL $db_table_prefix;

class test{
private $table_prefix;
public function __construct(){
global $db_table_prefix;
$this->table_prefix = $db_table_prefix;
}

public function test2(){
$stmt = $db->mysqli->prepare("SELECT id FROM {$this->table_prefix}users WHERE name = 'test'");
}
}

如何在查询本身中使用前缀?

最佳答案

更改您的代码:

   public function test2(){
$stmt = $db->mysqli->prepare("SELECT id FROM {$this->table_prefix}users WHERE name = 'test'");
}

   public function test2(){
$sql="SELECT id FROM {$this->table_prefix}users WHERE name = 'test'";
echo $sql;
$stmt = $db->mysqli->prepare($sql);
}

始终以这种方式使用它:首先创建字符串,然后显示字符串,然后调用数据库——然后您将确切地知道您在执行什么。

如果您确信您的代码可以正常工作,您可以随时注释掉 echo 语句。

如何调用这段代码?

关于php - 将 php 参数绑定(bind)到类中的 mysqli 准备语句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20799744/

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