php - 如何单独显示SQL结果

标签 php mysql function select methods

我目前正在制作一个网页,旨在显示数据库中的所有内容。所以我做了一个 SQL 命令,它只选择网页上 1 个特定字段所需的数据。

是否可以创建 SQL 命令,以便它一次获取页面的所有内容并且仍然能够单独显示它? 如果是这样,如何?谢谢

function dbGet() {
    global $conn;
    global $return;

    $sql = SELECT * FROM testTable;
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $return = $row["text"];
            return $return;
        }
    }
    else {
        echo "0 results";
    }
    // $conn->close();
}

最佳答案

你可以通过这种方式来识别记录是否存在。

function dbGet() {
    global $conn;
    // I am not sure what is the datatype of $return here.
    // if it's having a predefined format,
    // please push the records into $return instead of creating as an array,
    // which will be taken care by framework if any you are using.

    // global $return;
    $return = array('status' => false, records => []);

    $sql = "SELECT text FROM testTable";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $return['status'] = true;
        while($row = $result->fetch_assoc()) {
            $return['records'][] = $row["text"];
        }
    }

    // $conn->close();
    return json_encode($return);
}

关于php - 如何单独显示SQL结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891683/

相关文章:

Python的type()函数及其 'if'相关问题

javascript - 您如何使用一个代码块来定位网站上的所有类? (没有jquery)

php - Zend_Tool 移除 Controller / Action

php - 为什么我的查询使用 LIKE 和 php PDO 返回 0 行

php - 将值从 php 表单传递到 Paypal 金额

mysql - 表中的列值作为 View 中的列

mysql - 如何找出mysql数据库的用户名和密码

function - cmdlet 和函数有什么区别?

php - 从数据库获取数据,将其显示为 json 响应

php - laravel 5.2 将登录 session 数据插入自定义身份验证的数据库