我是使用PHP和Mysql数据库的新手。
我正在使用MAMP的phpmyadmin来创建数据库。
通过以下代码正常工作并建立与服务器的连接:
<?php
include "config.php";
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
echo 'Connected successfully.';
?>
但是,当我使用构造函数和类时,我遇到连接问题:
CONFIG.PHP:
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "scapik_users");
define("DB_PORT","8889");
?>
DbConnect.php
<?php
include "config.php";
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
?>
有人可以帮我纠正代码,我想我错了。
感谢。
答案 0 :(得分:1)
创建类
的对象$ obj = new DbConnect();
然后运行你的代码
<?php
include "config.php";
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
} else {
echo "connection successfull";
}
}
public function getDb(){
return $this->connect;
}
}
$obj = new DbConnect();
?>
可能会帮助你。
答案 1 :(得分:0)
尝试在构造函数中添加端口信息。由于您定义了它,可能需要连接到数据库,因为它可能只是在该端口上侦听。
使用完整的代码示例编辑我的答案 - 这可以在我这边工作
的config.php
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "test");
define("DB_PORT","3306");
DBConnect.php
<?php
include("config.php");
class DbConnect{
private $connect;
public function __construct(){
$this->connect = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
$db = new DbConnect();
print_r($db->getDb());
?>
这对我来说很好......所以我猜你的代码很好。
我猜您已经检查过您的用户凭据以及端口和内容......?
您是否尝试过设置错误报告?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
答案 2 :(得分:0)
充分利用面向对象的编程。初始化DbConnect
类时,请传递数据库参数。这将有助于您在整个课程中使用初始化变量。
<?php
include "config.php";
class DbConnect{
private $connect;
private $host;
private $username;
private $password;
private $database;
public function __construct($host, $username, $password, $database){
$this->host = $host;
$this->username= $username;
$this->password = $password;
$this->database = $database;
$this->connect = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
}
public function getDb(){
return $this->connect;
}
}
?>
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "scapik_users");
define("DB_PORT","8889");
?>
<?php
$host = DB_HOST;
$username = DB_USERNAME;
$password = DB_PASSWORD;
$database = DB_DATABASE;
#call this class as:
$class = new DbConnect($host, $username, $password, $database);
?>