Php 这有好处吗&;够安全吗?为特定页面建立附加连接
我已经让aleady使用mysqli作为主连接,但在特定页面上使用此mysql作为附加连接Php 这有好处吗&;够安全吗?为特定页面建立附加连接,php,mysql,mysqli,Php,Mysql,Mysqli,我已经让aleady使用mysqli作为主连接,但在特定页面上使用此mysql作为附加连接 class DBController { private $host = "localhost"; private $user = "root"; private $password = ""; private $database = "blog_samples"; function __construct() { $conn = $this->connectDB(); if(!e
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "blog_samples";
function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
它足够好和安全吗?有更好的方法吗?请解释一下它的优点。提前感谢您可以使用pdo方法进行连接和检查
try {
$conn = new PDO('mysql:host=localhost;dbname=database_name', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
根据您的php版本,其他方法还有mysql
或mysqli
// Try and connect to the database
$connection = mysqli_connect('localhost',$username,$password,$dbname);
// If connection was not successful, handle the error
if($connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
}
这已经很糟糕了,因为在最新的php5中,
mysql
api被弃用,在php7中被删除。您必须忘记它,使用mysqli
或PDO
。如前所述,停止使用mysql,改用mysqli或PDO。此外,您应该检查连接是否成功,并返回一些错误。使用try/catch。谢谢,看起来不错,但我已经将mysqli用作我的主连接。我在特定页面上使用的mysql仅用于秒连接。我错了吗?是的,您可以对特定页面和查询使用私有连接,但您应该检查您的php版本并查询您在查询中传递的连接。您拥有所有类型的灵活性。永远不要立即回显PHP错误。它在PHP5.6上仍然运行良好。