2016-01-15 45 views
1

我試圖從mysql數據庫獲取值並從php創建json。但我總是得到「虛假」的答覆。嘗試編碼json時出現錯誤php

我的查詢運行正常。

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

請幫我

+0

你什麼你json_encode之前獲得,當你'的var_dump($行)'對不對? –

+2

可能重複的[json \ _encode()返回false](http://stackoverflow.com/questions/19440529/json-encode-returns-false) –

+1

我得到這一個數組(0){} – Bangalore

回答

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

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); 
?> 
相關問題