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

标签 php mysql

我正在学习 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 地址连接到 sequel pro 而只能连接到 127.0.0.1?

最佳答案

Apache 服务器使用“127.0.0.1”而不是“localhost”

关于PHP - MYSQL - 测试数据库服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13870362/

相关文章:

php - 如何在字符串中从末尾的固定位置插入一个字符?

php - SSH登录到Netbeans中用于PHP的远程服务器

mysql - 在 bash 中从 mysql 获取计数值的最简单方法

mysql - 错误 "TypeError: not all arguments converted during string formatting"MySQLdb

java - 使用 servlet 和 jsp 进行数据库搜索

mysql - 在 SELECT 和 WHERE 子句上使用同一个表

mysql - 使用 Cloud SQL 在 Google App Engine 上扬 sails 起航

javascript - 我在日期范围选择器上得到 NaN 值

php - 不会从数据库中获取数据

php - $criteria->在 yii 中选择