PHP - MYSQL - 测试数据库服务器

nim*_*bus 5 php mysql sql-server webserver

我正在学习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();

?
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我是否忘记启用某些功能?

它可以解释为什么我不能通过我的IP地址连接到续集专业版,只能到127.0.0.1?

Jon*_*han 17

Apache服务器使用'127.0.0.1'而不是'localhost'


Ja͢*_*͢ck 10

您必须检查两个设置才能正常工作(假设您当然安装了MySQL服务器):

检查mysql.default_socketPHP配置中的值.

检查标题socket下MySQL配置文件中的值[mysqld].

这些价值必须相同; 如果不是,请更改一个以匹配另一个并重新启动相应的服务.