尝试编码json php时得到错误

时间:2016-01-15 03:20:48

标签: php mysql json

我想从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);
?>

请帮帮我

2 个答案:

答案 0 :(得分: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);
?>

答案 1 :(得分:0)

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