PHP-MYSQL-test数据库服务器
我正在学习PHP MYSQL并尝试创建一个数据库 我正在学习这个教程 到目前为止,我正在测试服务器是否可以访问MySQL。当我使用以下代码时PHP-MYSQL-test数据库服务器,php,mysql,Php,Mysql,我正在学习PHP MYSQL并尝试创建一个数据库 我正在学习这个教程 到目前为止,我正在测试服务器是否可以访问MySQL。当我使用以下代码时 ?php class RedeemAPI { private $db; // Constructor - open DB connection function __construct() { $this->db = new mysqli('localhost', 'username', 'password',
?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地址连接到sequel pro,而只能连接到127.0.0.1?要使其正常工作,您必须检查两个设置(当然,假设您安装了MySQL服务器): 在PHP配置中检查的值 检查MySQL配置文件中
[mysqld]
标题下的值
这些值必须相同;如果没有,请将其中一个更改为与另一个匹配,然后重新启动相应的服务。这似乎是一个常见的问题,因为谷歌搜索它会产生很多结果。我在我的两个linux机器上也经历过这一点(但从来没有在Windows下),在某个时候,我决定在所有开发服务器上使用127.0.0.1。基本上,localhost使用套接字连接到MySQL服务器,但是您的配置没有正确指向套接字文件 以下是一些您可能会觉得有用的资源: -基本上是@Jack的建议,但更深入 -以下是如何定位套接字文件(出于某种原因,我的php.ini路径错误,我认为您的情况可能与此类似)
我希望这会有所帮助。Apache服务器使用“127.0.0.1”而不是“localhost”是否安装了mysqli?它开始了吗?我将从一个更基本的例子开始。本地托管?Linux可能是一个设置服务器的挑战。我安装了mysql,我只通过本地地址连接到sequel pro,虽然我知道对于mysql,它可能与php.ini文件中的mysql.default_socket行相关。你应该在这个文件中检查一个类似的条目,这是有效的,但是你能解释一下为什么吗?我只是想弄清楚这一切。正如@mingos所解释的,这是一个常见的问题。“localhost”似乎没有指向Apache服务器中的正确套接字。这是通过使用127.0.0.1纠正的,与apache无关。Mysql通过“UNIX套接字”使用“localhost”,通过“TCP/IP”使用“127.0.0.1”。因此,如果使用“localhost”,请指定“mysql.sock”的正确路径。mysqli使用mysqli.default\u socket。和
pdo_mysql。pdo的默认_套接字。我们可以通过php-i|grep-F.default_socket