class Foo{
private $xml;
public function get_bar_xml($data){
$this->xml = $this->init_xml();
foreach($data as $key => $value){
$this->xml->addChild(key, $value);
}
return $this->prepare_xml();
}
//... set of similar methods following with different
// child node creation
private function init_xml(){
//$this->xml header generation
}
private function prepare_xml(){
//add footer nodes to $this->xml
//format output if neccesary
return $this->xml;
}
}
下面是打印的代码,但是如果我不提供用户名或密码,即如果我将其保留为空,如$ username ='';;
,则会在控制台上引发sql错误如果用户名或密码不正确或为空,我现在没有前端,该怎么办才能在控制台上打印错误
答案 0 :(得分:1)
为避免出现此错误“ 警告:mysqli_fetch_assoc()期望参数1为mysqli_result,请检查$ _POST是否不为空,并在其他情况下打印错误,仅用于测试。
要检查输入字段是否为空,请应用JavaScript验证以避免提交空白。
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!";
}
}
这里是一个示例,为简化代码,如果您有任何问题,请尝试打印变量,例如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);
}
?>
答案 1 :(得分:0)
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`;
}