【发布时间】:2014-03-22 11:56:30
【问题描述】:
这是我的 connection.php 文件
<?php
class Connection {
public $db_host = 'localhost';
public $db_user = 'root';
public $db_pass = '';
public $db_name = 'lybri';
public function connect() {
$this->connect_db = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
return true;
}
public function get_connection() {
return $this->connect_db;
}
}
$db = new Connection('localhost', 'user', 'pass', 'name');
$db->connect();
$connection = $db->get_connection();
此脚本运行正常,没有任何错误。
当我需要在另一个文件中运行 mysql 查询时,我只需将这个 connection.php 文件包含到该文件中并这样做。
mysqli_query($connection, "SELECT * FROM users WHERE username = '$username' AND password = '$hashedPassword' ");
我只需要知道,这是最好的做法吗?
谢谢。
【问题讨论】:
-
这个问题似乎跑题了,因为它属于Codereview Stackexchange
-
我会返回 true 或 false,而不是 exit();如果你问我,其他一切都很好。
-
@ShankarDamodaran - 哎呀我不知道。我可以将此问题移至 codereview 吗?
-
我认为模组会这样做,或者您可以删除它并在那里发布一个新的。
-
@tastro-耶!收到。谢谢。