我真的无法在标题中描述它,最好阅读其中的内容

时间:2019-12-31 06:55:30

标签: php web backend

我现在在做PHP时遇到了麻烦,并且濒临疯狂,因为我似乎无法显示我想要显示的数据。我得到的只是错误,当用户没有可用的任务时,我计划回显“无任务”(在我的情况下,数据库中已为用户准备了任务)。如果你们能指出我的代码/数据库出了什么问题,那将有很大的帮助。谢谢:D

页面功能的代码:

<?php

include("dbh.sys.php");
$user = $_SESSION['client_id'];
$sql = "SELECT * FROM incom_tasks WHERE taskUser=?;";
$stmt = $conn->stmt_init();

if(!$stmt = $conn->prepare($sql)) {
    echo '<p>SQL had an error!</p>';
} elseif($stmt = $conn->prepare($sql)) {
    //binder
    $stmt->bind_param("i", $user);
    //run!
    $stmt->execute();
    //get num rows
    $stmt->store_result();
    $num = $stmt->num_rows();
    $result = $stmt->get_result();
    if($num > 0){
        while($row = $result->fetch()){

        $points = $row['taskPoints'];
        $date = $row['taskDate'];
        $taskID = $row['taskID'];
        $sql = "SELECT * FROM tasks_list WHERE taskID=?;";
        $stmt = $conn->stmt_init();
        if(!$stmt = $conn->prepare($sql)) {
            echo '<p>SQL had an error!</p>';
        } elseif($stmt = $conn->prepare($sql)){
            $stmt->bind_param("i", $taskID);
            $stmt->execute();
            $stmt->store_result();

            $result = $stmt->get_result();

            while($row = $result->fetch()) {

                $name = $row['taskTitle'];
                $desc = $row['taskDesc'];

                //POINTS SUFFIX
                if($points == 1) {$pointsSuffix = "point";} else if($points > 1) {$pointsSuffix = "points";}

                echo('<div class="taskBox mdc-elevation--z1"><button class="material-icons taskCheck" title="Options..." onclick="markDone()">check_box_outline_blank</button><h2 class="taskName">'.$name.'</h2><p class="taskPoints">'.$points.' '.$pointsSuffix.'</p><p class="taskDesc">'.$desc.'</p><p class="taskDate">'.$date.'</p></div>');
               }    
            }
        }
    } else {
        echo '<p class="taskBox mdc-elevation--z1">No tasks for now!</p>';
    }
}

数据库处理程序的代码:

<?php

$servername = "localhost";
$dbUsername = "root";
$dbPass = "";
$dbName = "climate_webapp";

$conn = mysqli_connect($servername, $dbUsername, $dbPass, $dbName);

if(!$conn) {
    die("Connection failed! ". mysqli_connect_error());
}

数据库图片:

https://i.stack.imgur.com/45nhx.png

2 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改。我也在代码中添加了注释。

  

您应尽快关闭结果集和语句句柄   不再需要。这将有助于将资源返回到PHP和MySQL   快点。同样,如果您正在执行,他们也不会停止查询执行   将相同的变量分配给另一个查询,例如您正在执行的操作   与$stmt

include("dbh.sys.php");
$user = $_SESSION['client_id'];
$sql = "SELECT * FROM incom_tasks WHERE taskUser=?;";
$stmt = $conn->stmt_init();
$stmt = $conn->prepare($sql);
if(!$stmt) 
{
    echo '<p>SQL had an error!</p>';
    die(); //Will stop the code execution here
} 
$stmt->bind_param("i", $user);
$stmt->execute();
$stmt->store_result();
$num = $stmt->num_rows();
$result = $stmt->get_result();
$stmt->close(); // remember to close the statement else other stmt will not run.
if($num > 0)
{
    while($row = $result->fetch())
    {
        $points = $row['taskPoints'];
        $date = $row['taskDate'];
        $taskID = $row['taskID'];
        $sql = "SELECT * FROM tasks_list WHERE taskID=?;";
        $stmt = $conn->stmt_init();
        $stmt = $conn->prepare($sql);
        if(!$stmt) 
        {
            echo '<p>SQL had an error!</p>'; 
            die();
        } 
        $stmt->bind_param("i", $taskID);
        $stmt->execute();
        $stmt->store_result();
        $result = $stmt->get_result();
        $stmt->close(); // remember to close the statement else other stmt will not run.
        while($row = $result->fetch()) 
        {

            $name = $row['taskTitle'];
            $desc = $row['taskDesc'];

            //POINTS SUFFIX
            if($points == 1) {$pointsSuffix = "point";} else if($points > 1) {$pointsSuffix = "points";}

            echo('<div class="taskBox mdc-elevation--z1"><button class="material-icons taskCheck" title="Options..." onclick="markDone()">check_box_outline_blank</button><h2 class="taskName">'.$name.'</h2><p class="taskPoints">'.$points.' '.$pointsSuffix.'</p><p class="taskDesc">'.$desc.'</p><p class="taskDate">'.$date.'</p></div>');
        }
    }
} 
else 
{
    echo '<p class="taskBox mdc-elevation--z1">No tasks for now!</p>';
}

答案 1 :(得分:-1)

我认为您可以使用联接查询,而不是将单独的查询放入while循环中。它将减少代码中的循环次数,减轻数据库压力,并防止运行时出现隐藏错误。