donglu2761
2018-12-14 12:48
浏览 151
已采纳

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

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

图片转代码服务由CSDN问答提供 功能建议

  function loginuser(){
 $ results = array(); 
 $ errors = array(  ); 
 
 $ username ='abc'; 
 $ password ='123'; 
if($ username =''){
 array_push($ errors,“Username is required”); 
} \ 否则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); 
  } 
 
 
 
 
 
 
 
返回json_encode($ results); 
return json_encode($ errors); 
} 
    
 
 

下面是代码m打印,但是如果我不提供用户名或密码,它会在控制台上抛出一个sql错误,即如果我把它留空,如$ username ='';

我应该怎么做才能在控制台上打印错误,因为如果用户名为o,我现在没有前端 r密码不正确或为空

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 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 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);
    }
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题