PHP-MYSQL-测试数据库服务器 [英] PHP - MYSQL - test database server
问题描述
我正在学习PHP MYSQL并尝试创建数据库.
I'm learning PHP MYSQL and trying to create a database.
我正在关注本教程 http://www.raywenderlich.com/2941/how-to-to-write-a-simple-phpmysql-web-service-for-an-ios-app
到目前为止,我正在测试服务器是否可以访问MySQL.当我使用以下代码时.
So far I'm testing to see if the server has access to MySQL. When I use the following code.
?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
我也许忘记了启用某些功能吗?
Did I perhaps forget to enable something?
这也许可以解释为什么我无法通过IP地址连接到sequel pro,而只能连接到127.0.0.1?
It might explain why I cannot connect via my IP address to sequel pro and only to 127.0.0.1?
推荐答案
您必须检查以下两项设置才能使其正常运行(假设您已经安装了MySQL服务器):
There are two settings you have to check for this to work (assuming you have MySQL server installed of course):
检查 mysql.default_socket
在您的PHP配置中.
Check the value of mysql.default_socket
in your PHP configuration.
检查 socket
在[mysqld]
标题下的MySQL配置文件中.
Check the value of socket
in your MySQL configuration file under the [mysqld]
heading.
这些值必须相同;如果不是,请更改其中一个以匹配另一个,然后重新启动相应的服务.
Those values have to be identical; if they're not, change one to match the other and restart respective service.
这篇关于PHP-MYSQL-测试数据库服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!