将 Apache 连接到 mysql 数据库
Connecting apachec to mysql data base
作为作业的一部分,我正在尝试将我的 Apache 服务器连接到 MySql 数据库。
我已验证我的 Apache 正在使用以下代码:
<?php
class RedeemAPI {
// Main method to redeem a code
function redeem() {
echo "Hello, PHP!";
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?>
但是当我替换此代码以从我的数据库中读取时,出现以下错误:
<?php
class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost', 'username', 'password', 'promos');
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function redeem() {
// Print all codes in database
$stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
$stmt->execute();
$stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
while ($stmt->fetch()) {
echo "$code has $uses_remaining uses remaining!";
}
$stmt->close();
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?>
Error: Warning: mysqli::mysqli(): (HY000/2002): No such file or
directory in /Library/WebServer/Documents/promos/index.php on line 8
Warning: mysqli::autocommit(): Couldn't fetch mysqli in
/Library/WebServer/Documents/promos/index.php on line 9
Warning: mysqli::prepare(): Couldn't fetch mysqli in
/Library/WebServer/Documents/promos/index.php on line 20
Fatal error: Call to a member function execute() on a non-object in
/Library/WebServer/Documents/promos/index.php on line 21
请告诉我是什么原因造成的,我该如何解决。
要将 php 连接到 mysql,您需要放置以下代码来检查连接是否完成 -
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
让我知道现在出现了什么错误?
作为作业的一部分,我正在尝试将我的 Apache 服务器连接到 MySql 数据库。
我已验证我的 Apache 正在使用以下代码:
<?php
class RedeemAPI {
// Main method to redeem a code
function redeem() {
echo "Hello, PHP!";
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?>
但是当我替换此代码以从我的数据库中读取时,出现以下错误:
<?php
class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost', 'username', 'password', 'promos');
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function redeem() {
// Print all codes in database
$stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
$stmt->execute();
$stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
while ($stmt->fetch()) {
echo "$code has $uses_remaining uses remaining!";
}
$stmt->close();
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?>
Error: Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Library/WebServer/Documents/promos/index.php on line 8
Warning: mysqli::autocommit(): Couldn't fetch mysqli in /Library/WebServer/Documents/promos/index.php on line 9
Warning: mysqli::prepare(): Couldn't fetch mysqli in /Library/WebServer/Documents/promos/index.php on line 20
Fatal error: Call to a member function execute() on a non-object in /Library/WebServer/Documents/promos/index.php on line 21
请告诉我是什么原因造成的,我该如何解决。
要将 php 连接到 mysql,您需要放置以下代码来检查连接是否完成 -
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
让我知道现在出现了什么错误?