如何修复在PHP中使用while循环不显示任何数据

时间:2019-06-26 05:21:30

标签: php mysql vb.net hosting

我在PHP和MYSQL方面还很陌生,我试图获取状态栏中未更新(或接收)的所有数据库行。

在获取单行值时,此代码段非常适用。尝试在代码中插入while循环时。使用回显时显示空白响应。

保存时在编辑器中不显示错误消息,甚至显示“ x”。使用浏览器调用和测试url时,只是一个空白屏幕。我正在Hostinger中为我的php文件和mysql数据库使用文件管理器。

// array for JSON response
$response = array();

try {
    $sql = "SELECT * FROM Complaints where status=''";

    if (mysqli_query($conn, $sql)) {
        if (mysqli_num_rows(mysqli_query($conn, $sql)) > 0) {
            $response["success"] = 1;
            $response["message"] = "Request successfully created.";

            $row = mysqli_fetch_array(mysqli_query($conn, $sql));

            $photo = $row['id'];
            $name = $row['name'];
            $sin = $row['sin'];
            $cp_no = $row['cp_no'];
            $complaint = $row['complaint'];
            $landmark = $row['landmark'];

            $response["success"] = 1;
            $response["name"] = $name;
            $response["id"] = $photo;
            $response["sin"] = $sin;
            $response["cp_no"] = $cp_no;
            $response["complaint"] = $complaint;
            $response["landmark"] = $landmark;
        } else {
            //no items found
            $response["success"] = 0;
            $response["message"] = "No items found.";
        }

        echo json_encode($response);
        mysqli_close($conn);
    } else {
        //failed to fetch
        $response["success"] = 0;
        $response["message"] = "Oops! an Error occured.";

        //echoing JSON response
        echo json_encode($response);
    }
    mysqli_close($conn);
} catch (Exception $e) {
    echo "exception";
    // failed to fetch
    $response["success"] = 0;
    $response["message"] = 'message: ' . $e->getMessage();

    // echoing JSON response
    echo json_encode($response);
}

这是到目前为止我尝试过的代码

while ($row = mysqli_fetch_array(mysqli_query($conn, $sql))) {
    $photo = $row['id'];
    $name = $row['name'];

    $response["names"] = $name;
    $response["data"] = $photo;

    // $response[$i]=$row;
}

从数据库中获取的数据随后将转换为json格式。

然后将在VB.net上对其进行解析 该代码也可以在单行值上正常工作

VB.NET代码

    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader

    request = DirectCast(WebRequest.Create("http://u969542451.hostingerapp.com/Get_Data.php"), HttpWebRequest)

    response = DirectCast(request.GetResponse(), HttpWebResponse)
    reader = New StreamReader(response.GetResponseStream())

    Dim rawresp As String
    rawresp = reader.ReadToEnd()
    TextBox2.Text = rawresp.ToString
    MsgBox(rawresp.ToString)

    Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(rawresp)
    Dim dataItem = jsonResulttodict.Item("id")
    Dim nameItem = jsonResulttodict.Item("name")
    Dim SINItem = jsonResulttodict.Item("sin")
    Dim cpItem = jsonResulttodict.Item("cp_no")
    Dim compItem = jsonResulttodict.Item("complaint")
    Dim LMItem = jsonResulttodict.Item("landmark")
    Dim typeItem = "COMPLAINT"

    dgvSearch.Rows.Add(SINItem, nameItem, dataItem, cpItem, compItem, LMItem, typeItem)

1 个答案:

答案 0 :(得分:0)

你能这样尝试吗?

try {
    $response = array();
    $i=0;
    $sql = "SELECT * FROM Complaints where status=''";
    $data = mysqli_query($conn, $sql)
    if (mysqli_num_rows($data) > 0) {
        while ($row = mysqli_fetch_array($data)) {
            $photo = $row['id'];
            $name = $row['name'];

            $response[$i]["names"] = $name;
            $response[$i]["data"] = $photo;
            $i++;
        }
    }
}