使此执行在没有全局 pdo 对象的情况下工作

发布于 2024-11-29 03:00:28 字数 1013 浏览 3 评论 0原文

所以我试图替换我的全局变量,并且我坚持如何通过这个。

我的代码的一个小例子:

# index.php:

include "dbc.php";
echo cName(1);

# dbc.php:

try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF8", DB_USER, DB_PASS, $driver_options);
}
catch(PDOException $pe)
{
  die('Could connect to the database because: ' .$pe->getMessage());
}
function cName($cID){
 global $connect;
 $sql = $connect->prepare("SELECT name FROM cocos WHERE id=:id");
 $sql->bindValue(":id", $cID);
 $sql->execute();
 $sql = $sql->fetch();
 return $sql["name"];
}

如您所见,我在函数 cName() 内执行全局 $connect。我对所有其他功能也是如此,这看起来非常糟糕。

我就不能做点什么来不拥有它吗?如果我没有全局变量,它会说未定义函数。

我知道我可以添加一个参数 cName($connect, 1)

但这对我来说看起来和另一个一样草率。由于我有很多函数,我总是需要为此连接添加一个额外的参数。

那么,如果没有额外的参数或任何全局变量,我该怎么办呢?

我想也许你可以创建一个函数/类/对象来处理与数据库的所有通信,但我不知道从哪里开始以及应该如何完成?

类似于:

dbQuery($query);

但话又说回来,您无法指定bindValue(),并且在某些情况下您需要绑定比上面示例中更多的值。

真的希望能找到解决方案,谢谢转发,

如有问题请在评论中提问

So I am trying to replace my global variables, and I am stuck to how to get pass this.

A little example of my code:

# index.php:

include "dbc.php";
echo cName(1);

# dbc.php:

try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF8", DB_USER, DB_PASS, $driver_options);
}
catch(PDOException $pe)
{
  die('Could connect to the database because: ' .$pe->getMessage());
}
function cName($cID){
 global $connect;
 $sql = $connect->prepare("SELECT name FROM cocos WHERE id=:id");
 $sql->bindValue(":id", $cID);
 $sql->execute();
 $sql = $sql->fetch();
 return $sql["name"];
}

As you can see I do global $connect inside the function cName(). And so do i do to all my other functions, which looks really bad.

Can't i do anything to not have it? It says undefined function if I dont have the global variable.

I know I can add an parameter, cName($connect, 1)

But this, for me, looks as sloppy as the other. And as I have lots of functions, I would always need to include an extra parameter for this connection.

So what can I do about this, not having an extra parameter or any global vars?

I have thought maybe you could make one function/class/object that handles all the communications to the database, but I dont know where to start and how it should be done?

Something like:

dbQuery($query);

but then again, you cant specify bindValue() and in some cases you need to bind more than one value than in the example above.

Really hope for an solution for this, thanks in forward

Please ask in comments if questions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-12-06 03:00:28

我会在序言中说,我的 php 充其量只是平均水平......也就是说:

class Database 
{ 
    public $dbh;   // handle of the db connexion 
    private static $dsn  = 'mysql:host=localhost;dbname=DBNAME HERE'; 
    private static $user = 'DBUSERNAME HERE'; 
    private static $pass = 'PASSWORD HERE'; 


    public function factory() { 
      return $dbh = new PDO(self::$dsn,self::$user,self::$pass); 
    } 
} // end of Database Class

然后在您的函数中,调用 facotry() 实例。

function cName($cID)
{ 

   // call Database::factory, gives you access to methods and properties
   $db= Database::factory(); 

   $sql = $db->prepare("SELECT first_name FROM users WHERE id=:id"); 
   $sql->bindValue(":id", $cID); 
   $sql->execute(); 
   $sql = $sql->fetch(); 

   // return the values
   return $sql["first_name"];

} // end of cName function

如果您想让数据库类更加模块化、更易于配置等,您可以扩展数据库类...

希望有所帮助...再说一次,我不是专家。

I will preface by saying I am at best average at php.... That being said:

class Database 
{ 
    public $dbh;   // handle of the db connexion 
    private static $dsn  = 'mysql:host=localhost;dbname=DBNAME HERE'; 
    private static $user = 'DBUSERNAME HERE'; 
    private static $pass = 'PASSWORD HERE'; 


    public function factory() { 
      return $dbh = new PDO(self::$dsn,self::$user,self::$pass); 
    } 
} // end of Database Class

Then In your Function, call the facotry() instance.

function cName($cID)
{ 

   // call Database::factory, gives you access to methods and properties
   $db= Database::factory(); 

   $sql = $db->prepare("SELECT first_name FROM users WHERE id=:id"); 
   $sql->bindValue(":id", $cID); 
   $sql->execute(); 
   $sql = $sql->fetch(); 

   // return the values
   return $sql["first_name"];

} // end of cName function

You could extend the Database class if you wanted to make it more modular, easier to conifg, etc...

Hope that helps... again, I am not an expert.

一梦等七年七年为一梦 2024-12-06 03:00:28

使用返回 PDO 实例的静态方法怎么样?它需要一行,与全局 $connection 相同,如果您使用 multiton 而不是singleton,你可以选择你想要的数据库。

What about to use a static method which return the instance of the PDO? It takes one row, same as global $connection and if you'll use multiton instead of singleton, you can choose which database you want..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文