【问题标题】:Getting false when tried to encode json php尝试编码 json php 时出现错误
【发布时间】:2016-04-20 14:55:19
【问题描述】:

我试图从 mysql db 获取值并从 php 创建 json。但我总是得到“假”作为回复。

我的查询工作正常。

<?php
header("Content-type: text/html; charset=utf-8"); 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$charset="UTF8";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sth = mysqli_query("select
    a.id,
    a.fname,
a.mname,a.lname,a.country,a.city,a.dob,a.role,
    a.email,
    b.mobile,b.skypeid,b.address,b.languages,
c.height,
c.width,
c.skin,
c.bust,
c.waist,
c.hips,
c.shoesize,
c.hair,
c.eye,
c.comments,
d.movie,
d.advertisement,
d.brandpromotional,
d.danceshow,
d.runway,
d.catalog,
d.editorial,
d.fit,
d.casual,
d.corporate,
d.swimwear,
d.fitness,
d.magazine,
d.lingerie,
d.glamour,
d.alternative,
d.hair,
d.legs,
d.hands,
d.webmodel,
d.social,
d.experience

from
    basicinfo a
        join contactdetails b
            on a.email=b.email
         join measurements c
             on a.email = c.email
         join areainterest d
on a.email=d.email
where a.role='Model'");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
    $rows[] = $r;
}
print json_encode($rows);
?>

请帮帮我

【问题讨论】:

  • 在 json_encode 之前var_dump($rows) 会得到什么?
  • 我得到这个数组(0){}
  • 与stackoverflow.com/questions/19440529/…不重复阅读两个问题
  • @Bangalore 除了应用程序的细节之外,这是相同的问题,几乎可以保证具有相同的原因和解决方案。请参阅有关该问题的已接受答案,以帮助您调试问题。

标签: php mysql json


【解决方案1】:

这个正在工作..

<?php
header("Content-type: text/html; charset=utf-8"); 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$charset="UTF8";

// Create connection
$connection = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
} 

$sth = "select
    a.id,
    a.fname,
a.mname,a.lname,a.country,a.city,a.dob,a.role,
    a.email,
    b.mobile,b.skypeid,b.address,b.languages,
c.height,
c.width,
c.skin,
c.bust,
c.waist,
c.hips,
c.shoesize,
c.hair,
c.eye,
c.comments,
d.movie,
d.advertisement,
d.brandpromotional,
d.danceshow,
d.runway,
d.catalog,
d.editorial,
d.fit,
d.casual,
d.corporate,
d.swimwear,
d.fitness,
d.magazine,
d.lingerie,
d.glamour,
d.alternative,
d.hair,
d.legs,
d.hands,
d.webmodel,
d.social,
d.experience

from
    basicinfo a
        join contactdetails b
            on a.email=b.email
         join measurements c
             on a.email = c.email
         join areainterest d
on a.email=d.email
where a.role='Model'";
  $result = mysqli_query($connection, $sth) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $emparray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    //close the db connection
    mysqli_close($connection);
?>

【讨论】:

    【解决方案2】:

    mysqli_query 缺少连接参数,但由于您是new mysqli 而不是mysqli_connect,看起来您需要以不同的方式查询数据......更新代码的最简单方法是这样做:

    改变这个:

    $sth = mysqli_query("select
    

    到这里:

    $sth = $conn->query("select
    

    然后改变这个:

    while($r = mysqli_fetch_assoc($sth)) {
    

    到这里:

    while($r = $sth->fetch_assoc()) {
    

    PHP 有三种不同的方式可以连接到数据库:面向对象样式、过程样式或使用 PDO。该代码混合了面向对象和过程样式,因此无法按预期工作。 mysqli_query 也缺少作为第一个参数的链接变量。

    在此处阅读更多信息:http://php.net/mysqli_query

    【讨论】:

      猜你喜欢
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多