PHP数组不返回值

时间:2015-04-09 00:14:33

标签: php mysql arrays

我有一个PHP文件,应该从mysql返回一个数组。问题是数组没有通过。

如果我查询2列或更多列,则数组为空。

<?php 
//conection: 
$mysqli = mysqli_connect("localhost","root","","imoveis") or die("Error " . mysqli_error($mysqli)); 


//consultation: 

$query = "SELECT * FROM saomiguel" or die("Error in the consult.." . mysqli_error($mysqli)); 


//execute the query. 

$result = mysqli_query($mysqli, $query);

//display information: 

$response = array();

if (mysqli_num_rows($result) > 0) {
    // looping through all results
    // products node    
    $response["saomiguel"] = array();       

      while ($row = mysqli_fetch_array($result)) {
        // temp user array
        $product = array();     
        $product["id"] = $row["id"];
        $product["second"] = $row["second"];

        // push single product into final response array
        array_push($response["saomiguel"], $product);
    }
    // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);

} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);  

        // closing db connection
        mysqli_close($mysqli);

}
?>

如果我在while循环中设置echo响应,它只返回一行。

注意:

JUst发现了错误!在我的一个专栏里面,我有“Imóvel”'给我一个错误!我用它作为varchar,大小75,charset latin1。

charset能解决我的问题吗?

编辑:

通过添加

解决问题

$ mysqli-&GT; set_charset( “UTF8”);

1 个答案:

答案 0 :(得分:0)

您应该删除此行

//显示信息:

$ response = array();

你可以试试这个。如果有效,请告诉我。

<?php 
//conection: 
$mysqli = mysqli_connect("localhost","root","","imoveis") or die("Error " . mysqli_error($mysqli)); 


//consultation: 

$query = "SELECT * FROM saomiguel" or die("Error in the consult.." . mysqli_error($mysqli)); 


//execute the query. 

$result = mysqli_query($mysqli, $query);


if (mysqli_num_rows($result) > 0) {
    // looping through all results
    // products node    
    $response["saomiguel"] = array();       

      while ($row = mysqli_fetch_array($result)) {
        // temp user array
        $product = array();     
        $product["id"] = $row["id"];
        $product["second"] = $row["second"];

        // push single product into final response array
        array_push($response["saomiguel"], $product);
    }
    // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);

} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);  

        // closing db connection
        mysqli_close($mysqli);

}
?>