将mysql_ *转换为mysqli_ *时的问题。该代码不再有效

时间:2015-08-11 14:45:50

标签: php mysqli

我有以下代码,我从互联网上获取“检查用户名可用性”。

它是用mysql_*编写的,但我想将其转换为mysqli_*,因为PHP7中不再支持mysql_*

但转换后它不起作用,用户名不会被检查,我总是变成绿色勾选

我尝试了OOP编程也是程序,最后我在mysql的最后添加了一个“i”for mysqli。这是mysql_ *的代码。任何人都可以帮忙转换吗?

文件 dbConnector.php

 <?php

 class DbConnector {

var $theQuery;
var $link;

function DbConnector(){

    // Get the main settings from the array we just loaded
    $host = 'localhost';
    $db = 'accesspi';
    $user = 'root';
    $pass = '';

    // Connect to the database
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));

   }

  //*** Function: query, Purpose: Execute a database query ***
function query($query) {

    $this->theQuery = $query;
    return mysql_query($query, $this->link);

 }

   //*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

    return mysql_fetch_array($result);

  }
  //*** Function: close, Purpose: Close the connection ***
  function close() {

     mysql_close($this->link);
 }
}
?>

档案 check.php

<?php
include("dbConnector.php");
$connector = new DbConnector();

$username = trim(strtolower($_POST['username']));
$username = mysql_escape_string($username);

$query = "SELECT username FROM admin WHERE username = '$username' LIMIT 1";
$result = $connector->query($query);
$num = mysql_num_rows($result);

echo $num;
mysql_close();
?>

使用javascript代码

文件 index.php
<link href="../style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript"    src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
$(document).ready(function(){
$('#username').keyup(username_check);
});

function username_check(){  
var username = $('#username').val();
if(username == "" || username.length < 4){
$('#username').css('border', '3px #CCC solid');
$('#tick').hide();
}else{

jQuery.ajax({
type: "POST",
url: "check.php",
data: 'username='+ username,
cache: false,
success: function(response){
if(response == 1){
$('#username').css('border', '3px #C33 solid'); 
$('#tick').hide();
$('#cross').fadeIn();
}else{
$('#username').css('border', '3px #090 solid');
$('#cross').hide();
$('#tick').fadeIn();
     }

  }
  });
  }
   }

</script>

<style>
#username{
padding:3px;
font-size:18px;
border:3px #CCC solid;
}

#tick{display:none}
#cross{display:none}


</style>
</head>

 <body>


 Username: <input name="username" id="username" type="text" />
<img id="tick" src="tick.png" width="16" height="16"/>
<img id="cross" src="cross.png" width="16" height="16"/>

</body>
</html>

转换后的文件dbConnector。 PHP

 <?php

 class DbConnector {

 var $result;
 var $conn;

 function DbConnector(){

    // Get the main settings from the array we just loaded
    $host = 'localhost';
    $db = 'accesspi';
    $user = 'root';
    $pass = '';

    // Connect to the database
    $conn= mysqli_connect($host, $user, $pass, $db);

  }

 //*** Function: query, Purpose: Execute a database query ***
 function query($query) {

        $result = $conn->query($query);

  }

  //*** Function: fetchArray, Purpose: Get array of query results ***
  function fetchArray($result) {

    return $result->fetch_array(MYSQLI_ASSOC);

  }

  //*** Function: close, Purpose: Close the connection ***
  function close() {

  $conn->close();

}

 }

 ?>

和check.php

     <?php
   include("dbConnector.php");
   $connector = new DbConnector();
   if (isset($_POST['username'])){
   $username = trim(strtolower($_POST['username']));
   $username = $connector->real_escape_string($username);

    $query = "SELECT username FROM admin WHERE username = '$username' LIMIT 1";
   $result = $connector->query($query);
   $num = $result->num_rows;

  echo $num;
  $connector->close();
  }
  ?>

1 个答案:

答案 0 :(得分:-2)

试试这个:

<?php
class DbConnector {

    private $theQuery;
    private $link;

    public function __construct(){

        // Get the main settings from the array we just loaded
        $host = 'localhost';
        $db = 'accesspi';
        $user = 'root';
        $pass = '';

        // Connect to the database
        $this->link = new mysqli($host, $user, $pass,$db);
        register_shutdown_function(array(&$this, 'close'));
    }

    /**
     * Function: query, Purpose: Execute a database query
     * @param $query
     * @return bool|mysqli_result
     */
    public function query($query) {

        $this->theQuery = $query;
        return $this->link->query($query);

    }

    /**
     * Function: fetchArray, Purpose: Get array of query results
     * @param $result mysqli_result
     * @return mixed
     */
    public function fetchArray($result) {
        return $result->fetch_array();
    }

    /**
     * Function: close, Purpose: Close the connection
     */
    public function close() {
        if(isset($this->link)){
            $this->link->close();
            unset($this->link);
        }
    }

    /**
     * @return mysqli
     */
    public function getLink(){
        return $this->link;
    }


}

check.php:

<?php
include("dbConnector.php");
$connector = new DbConnector();

$username = filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
$username = $connector->getLink()->real_escape_string($username);

$query = "SELECT username FROM admin WHERE username = '$username' LIMIT 1";
$result = $connector->query($query);
$num = $result->num_rows;

echo $num;
$connector->close();