小编R K*_*R K的帖子

PHP中未显示预期的异常消息

我想在PHP中编写一个带有数据库连接和异常处理的程序.如果我插入一个不正确的用户名,那么它应该显示相应的错误消息,如果我插入不正确的数据库,它应该显示相应的错误消息.

但是在下面的程序中,无论是插入不正确的数据库还是不正确的用户名,它都只显示消息"无法连接到数据库".

<?php
   $hostname = "localhost";
   $username = "root1";
   $password = "";
   $database = "php_thenewboston";
   $conn = mysqli_connect($hostname,$username,$password);
   $conn_db = mysqli_select_db($conn,$database);
   class ServerException extends Exception{}  
   class DatabaseException extends Exception{}
   try{
       if(!$conn){
           throw new ServerException('Could not connect to server.');
       }elseif(!$conn_db){
           throw new DatabaseException('Could not connect to database.');
       }else{
           echo "Connected.";
       }
   }catch(ServerException $ex){
       echo "Error :".$ex->getMessage();        
   }catch(DatabaseException $ex){
       echo "Error :".$ex->getMessage();
   }
?>
Run Code Online (Sandbox Code Playgroud)

我是PHP的初学者.如有任何疑问,请在下面评论.

编辑

正如@Hatef所要求的那样var_dump,$conn当用户名不正确,密码正确且数据库名称正确时

object(mysqli)#1 (19) {
  ["affected_rows"]=>
  int(-1)
  ["client_info"]=>
  string(79) "mysqlnd 5.0.12-dev - 20150407 …
Run Code Online (Sandbox Code Playgroud)

php mysql

15
推荐指数
1
解决办法
485
查看次数

如何将以下一系列for循环缩小为不那么紧凑的代码?

我有以下代码遵循循环模式,我有一种感觉,代码可以缩小为代码或任何不那么难看的代码的递归,但我无法弄明白.

我想从跑了六圈在另一个内1000,以10000在JavaScript中,我看,如果能够再缩小代码.

我是编码初学者,但我可以接受各种方法.

我正在更新代码,因为以前的代码可能会让某些用户感到暧昧.

function dummyFunc(x,y){
    if( some logic for x == some logic for y){
         return true;
    }
    return false;
}

for(var i = 1000;i < 10000;i++){
  for(var j = 1000;j < 10000;j++){
    if(dummyFunc(i,j)){
      for(var k = 1000;k < 10000;k++){
        if(dummyFunc(j,k)){
          for(var l = 1000;l < 10000;l++){
            if(dummyFunc(k,l)){
              for(var m = 1000;m < 10000;m++){
                if(dummyFunc(l,m)){
                  for(var n = 1000;n < 10000;n++){
                     if(dummyFunc(m,n)){
                        break;
                     }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript optimization

6
推荐指数
1
解决办法
319
查看次数

通过删除属性启用和禁用按钮

我想编写一个程序来通过删除属性jQuery 代码启用和禁用复选框上的按钮。

我试过了,但没有用。

 $(document).ready(function() {
   $('#myCheckBox').on('change', function() {
     var x = $(this).attr('value');
     if (x == 'on') {
       $('#myButton').removeAttr('disabled');
     } else if (x == undefined) {
       $('#myButton').attr('disabled');
     }
   });
 });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<label for="myCheckBox">
  I agree with terms and conditions
  <input type="checkbox" id="myCheckBox" />
</label>
<br />
<br />
<input type="button" id="myButton" value="Submit" disabled />
Run Code Online (Sandbox Code Playgroud)

我是 jQuery 的初学者,如有任何疑问,请在下面发表评论。

javascript jquery

5
推荐指数
1
解决办法
3322
查看次数

通过php和jQuery验证电子邮件

我想通过使用php和jquery验证我输入的电子邮件字段.但我无法弄清楚我的计划中的错误.

下面是代码 index.html

<!DOCTYPE html>
<html lang="en">
    <head>
         <title>
         </title>
         <meta charset="utf-8" />
         <link rel="stylesheet" type="text/css" href="css/custom.css" />
    </head>
    <body>
        <p>Email: <input type="text" id="myInput" /></p>
        <p id="feedbackDiv"></p>
        <p><input type="button" id="myButton" value="Check" /></p>

    <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
    <script type="text/javascript" src="js/custom.js" ></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

下面是代码 custom.js

$(document).ready(function(){
    $('#myButton').on('click',function(){
        var x = $('#myInput').val();

        function validateEmail(mail){
            $.post('php/email.php',{email_value_server: mail},function(returnData){
                $('#feedbackDiv').html(returnData);
            });
        }

        $('#myInput').focusin(function(){
            if(x === ''){
                $('#feedbackDiv').html('Please fill the email address.');
            }else{
                validateEmail(x);
            }
        }).blur(function(){
            $('#feedbackDiv').html('');
        }).keyup(function(){
            validateEmail(x);
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

下面是代码 email.php

<?php
    $y …
Run Code Online (Sandbox Code Playgroud)

javascript php jquery

2
推荐指数
1
解决办法
1404
查看次数

':button'和'button'之间的区别作为选择器

<!DOCTYPE html>
<html lang="en">
    <head>
         <title>
         </title>
         <meta charset="utf-8" />
         <link rel="stylesheet" type="text/css" href="css/custom.css" />
    </head>
    <body>
        <button value="">Button 1</button>
        <button value="">Button 2</button>
        <button value="">Button 3</button>      
    <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
    <script type="text/javascript" src="js/custom.js" ></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

下面是custom.js中的代码

$(':button').on('click',function(){
    alert('Hello');
});
Run Code Online (Sandbox Code Playgroud)

当我将custom.js中的代码更改为

$('button').on('click',function(){
    alert('Hello');
});
Run Code Online (Sandbox Code Playgroud)

他们这样做是点击显示警报同样的工作,但我想知道的区别'button'':button'

javascript jquery

1
推荐指数
1
解决办法
132
查看次数

jQuery中的样式代码无法正常工作

<!DOCTYPE html>
<html lang="en">
    <head>
         <title>
         </title>
         <meta charset="utf-8" />
         <link rel="stylesheet" type="text/css" href="css/custom.css" />
    </head>
    <body>
        <button id="myButton">My Button</button><br /><br />
        <span id="myPara"> My paragraph</span>      
    <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
    <script type="text/javascript" src="js/custom.js" ></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

下面是代码 custom.css

#myPara{
    border: 1px solid black;
    color: black;
    padding: 5px;
}
Run Code Online (Sandbox Code Playgroud)

下面是代码 custom.js

$('#myButton','#myPara').on('click',function(){
    $(this).css('border-color','white').css('background','black').css('color','white');
});
Run Code Online (Sandbox Code Playgroud)

我想应用给定的css,如jquery代码中所示.

它也没有在控制台中给出任何错误,这使我更难找到错误.

在我的浏览器中,点击时没有显示css效果.

我是jQuery的初学者.

html javascript css jquery

0
推荐指数
1
解决办法
64
查看次数

标签 统计

javascript ×5

jquery ×4

php ×2

css ×1

html ×1

mysql ×1

optimization ×1