我正在学习PHP MYSQL,并试图建立一个数据库。
我下面这个教程http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app
到目前为止,我测试,看看如果服务器有到MySQL访问。 当我使用下面的代码。
?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();
?
我得到以下错误:
Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/user/Sites/promos/index.php on line 8
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2002): No such file or directory in /Users/user/Sites/promos/index.php on line 8
Warning: mysqli::autocommit() [mysqli.autocommit]: Couldn't fetch mysqli in /Users/user/Sites/promos/index.php on line 9
Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in /Users/user/Sites/promos/index.php on line 20
Fatal error: Call to a member function execute() on a non-object in /Users/user/Sites/promos/index.php on line 21
难道我也许忘记启用的东西吗?
这或许可以解释为什么我无法通过我的IP地址,续集专业,只为127.0.0.1连接?