PHP Json编码适用于一个数据库,但不适用于其他数据库

时间:2015-02-06 17:09:03

标签: php mysql arrays json angularjs

我是angularjs的新手,我试图从mysql获取数据,将其编码为json,并将其与angularjs ng-repeat一起使用。

我的代码适用于一个数据库但不适用于其他数据库。为了测试,我确实将行转储为数组和json。我可以在连接到一个数据库时看到数组和json,但在连接到其他数据库时只能看到数组。

我无法弄清楚原因。我的两个数据库的默认排序规则是utf8_unicode_ci。

这是我的测试页面,它转储数组和json:

<?php
include('data.php');
$database= new Database();
$database->query('SELECT Name,City,Address1,PostalCode,LowRate,HighRate,Country,Id from ratedmain Limit 10');
$rows= $database->resultset(PDO::FETCH_ASSOC);
$json=json_encode($rows);
$count=$database->rowcount();
echo $json;
var_dump($rows);
?>     
    </body>
</html>

这只给了我数组而不是json。下面是输出:(例如,下面只添加一个)

array (size=10)
  0 => 
    array (size=8)
      'Name' => string 'Resorts Casino Hotel Atlantic City' (length=34)
      'City' => string 'Atlantic City' (length=13)
      'Address1' => string '1133 Boardwalk' (length=14)
      'PostalCode' => string '08401' (length=5)
      'LowRate' => string '49.0000' (length=7)
      'HighRate' => string '79.0000' (length=7)
      'Country' => string 'US' (length=2)
      'Id' => string '1' (length=1)

但是当我将其更改为其他DB时,我的输出如下:(添加了一段json和一个数组值作为示例)

[{"customerName":"Atelier graphique","addressLine1":"54, rue Royale","city":"Nantes","state":null,"postalCode":"44000","country":"France","creditLimit":"21000"},{"customerName":"Signal Gift Stores","addressLine1":"8489 Strong St.","city":"Las Vegas","state":"NV","postalCode":"83030","country":"USA","creditLimit":"71800"},
  0 => 

    array (size=7)
      'customerName' => string 'Atelier graphique' (length=17)
      'addressLine1' => string '54, rue Royale' (length=14)
      'city' => string 'Nantes' (length=6)
      'state' => null
      'postalCode' => string '44000' (length=5)
      'country' => string 'France' (length=6)
      'creditLimit' => string '21000' (length=5)

这是我的构造函数:

<?php
class Database{
private $host=DB_HOST;
private $user=DB_USER;
private $pass=DB_PASS;
private $dbname=DB_NAME;
private $stmt;
private $dbh;
private $error;

public function __construct()
{
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT    => true,
PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
//PDO::ATTR_EMULATE_PREPARES, false
);
// Create a new PDO instanace
    $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);

}



    public function query($query){
        $this->stmt = $this->dbh->prepare($query);
    }



    public function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute(){
        return $this->stmt->execute();
    }

    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }


    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount(){
        return $this->stmt->rowCount();
    }


}

希望你能帮助我。

0 个答案:

没有答案