dragon202076 2015-02-06 17:09
浏览 20

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

I am new to angularjs and i am trying to get the data from mysql encode it as json and use it with angularjs ng-repeat.

My code works with one database but not with other.In order to test I did dump the rows as array and the json. I can see the array and json when connected to one database but only array when connected to other database.

I could not figure out why. The default collation of my both databases is utf8_unicode_ci.

Here is my test page which dumps array and 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>

This gives me only array but not json. Below is the out put:(added only one below for example)

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)

But when I change this to other DB my output is as below: (added a snippet of json and one array value as an example)

[{"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)

here is my constructor:

<?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();
    }


}

Hope if you can help me.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 树莓派安卓APK系统签名
    • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
    • ¥65 汇编语言除法溢出问题
    • ¥15 Visual Studio问题
    • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波