donglu2761
donglu2761
2018-12-14 12:48
浏览 60

如何在控制台上打印错误?

function loginuser() {
    $results=array();
    $errors = array();

        $username = 'abc';
        $password = '123';
if ($username='') {
            array_push($errors, "Username is required");
        }
        else if ($password='') {
            array_push($errors, "Password is required");
        }


            $query = "SELECT * FROM users WHERE user_name='$username' AND user_pass='$password'";
            $result = selection($query);

        while ($row = mysqli_fetch_assoc($result)) {
        $results[] = row_jsonify($row);
    }










return json_encode($results);
return json_encode($errors);
}

Below is the code m printing but its throwing a sql error on console if i dont give a username or password i.e if i leave it empty like $username=' ';

what should i do here to print the errors on console as i dont have front end right now if the username or password is incorrect or empty

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douying6206
    douying6206 2018-12-14 13:40
    已采纳
    showErrsInConsole(['err1', 'error2', 'error3']);
    
    function showErrsInConsole(array $errors)
    {
        array_walk($errors, function (&$err) {
            $err = 'echo ' . $err;
        });
        $errors = implode(';', $errors);
        $exec = "gnome-terminal -x bash -c '$errors; sleep 1; read -n 1 -p \"press any key to close\"';";
        `$exec`;
    }
    
    点赞 评论
  • duandi5328
    duandi5328 2018-12-14 12:55

    To avoid this error "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, check if $_POST is not empty and print errors in else case just for testing.

    To check if input fields are empty apply JavaScript validations to avoid empty from submission.

    if(!empty($_POST["login"])) {
        $result = mysqli_query($conn,"SELECT * FROM users WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
        $row  = mysqli_fetch_array($result);
        if(is_array($row)) {
        $_SESSION["user_id"] = $row['user_id'];
        } else {
        $message = "Invalid Username or Password!";
        }
    }
    

    Here is an example,to simplify your code, if you have any problem then try printing variables e.g. var_dump & print_r

    <?php
    session_start();
    $conn = mysqli_connect("localhost","root","","database_ex");
    
    $message="";
    if(!empty($_POST["login"])) {
        $result = mysqli_query($conn,"SELECT * FROM users WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
        $row  = mysqli_fetch_array($result);
        if(is_array($row)) {
        $_SESSION["user_id"] = $row['user_id'];
        } else {
        $message = "Invalid Username or Password!";
        }
    }else{
      //Print error message here if no username or password provided;
      echo "Invalid credentials"; 
      print_r($_POST);
      var_dump($_POST);
    }
    ?>
    
    点赞 评论

相关推荐