从不建议使用sql_query()转换后的sqli_query()问题

时间:2019-03-01 16:17:31

标签: php mysqli

settings.php

<?php
$website_directory = "c:/xampp/htdocs/cyberbot/";

$database_server = "localhost";
$database_port = 3306;
$database_database = "cyberbot";
$database_user = "root";
$database_password = "password";
?>

mysql.php

<?php
class mysql {
    function __construct() {
        $this->errors = "";
    }
    function Connect($hostname,$port,$database,$username,$password) { // DATABASE CONNECTION */
        $result = mysqli_connect($hostname.":".$port,$username,$password) or die ('Connection to database failed.'); // DATABASE CONNECTION
        if (!$result) {
            $this->Error('Connection to database server at: '.$this->hostname.' failed.');
            return false;
        }
        else {
            $sel_db=mysqli_select_db($result, $database); // SELECT THE DATABASE
            if (!$sel_db) die("unable to select db, does the database <b>".$database."</b> exist?") ;
            mysqli_query($result,"set autocommit=1"); // RUN IN AUTOCOMMIT MODE FOR INNODB TABLES
            return $result;
        }
    }
    function pconnect() { // PERSISTENT CONNECTION
        $result = mysqli_pconnect($this->hostname, $this->username, $this->password);

        if (!$result) {
            echo 'Connection to database server at: '.$this->hostname.' failed.';
            return false;
        }
        return $result;
    }
    function Query($query,$query_no="") { // THE METHOD TO EXECUTE QUERIES
        //echo "Sql QUERY: ".$query."<br>";
        //$result = mysqli_query($query) or die("Query failed:  $query<br><br>".mysqli_error());
        $result = mysqli_query($query) or die(ShowSQLError($query_no,$query));
        return $result;
    }
    function FetchArray($result) { // A METHOD TO RETURN THE RESULT AS AN ARRAY
        return mysqli_fetch_array($result);
    }
    function FetchAssoc($result) { // AN ALTERNATIVE METHOD TO RETURN AS AN ASSOCIATIVE ARRAY
        return mysqli_fetch_assoc($result);
    }
    function FetchRow($result) { // AN ALTERNATIVE METHOD TO RETURN ROWS
        $query = mysqli_fetch_row($result);
        return $result;
    }
    function ReturnQueryNum() { // A METHOD TO RETURN THE QUERY NUMBER
        return $this->query_num;
    }
    function NumRows($result) { // A METHOD TO RETURN THE NUMBER OF ROWS IN A RESULT
        return mysqli_num_rows($result);
    }
    function AffectedRows() { // A METHOD TO DETERMINE HOW MANY ROWS WERE AFFECTED BY THE QUERY
        return mysqli_affected_rows();
    }
    function GetColumns($result) {
        //return mysqli_fetch_field($result, $i);
        $i = 0;
        //echo mysqli_num_fields($result);
        $fields_arr[]="";
        for ($i=0;$i<mysqli_num_fields($result);$i++) {
            $meta= mysqli_fetch_field($result, $i);
            array_push($fields_arr,$meta->name);
        }
        return $fields_arr;
    }
    function LastInsertId() { // A METHOD TO OBTAIN THE LAST INSERTED AUTOINCREMENT ID
        return mysqli_insert_id();
    }
    function Begin() { // A METHOD TO START A TRANSACTION
        mysqli_query("set autocommit=0");
    }
    function commit() { // COMMIT
        mysqli_query("commit");
    }
    function Rollback() { // ROLLBACK
        mysqli_query("rollback");
    }
    function Error($err) {
        $this->errors.=$err."<br />";
    }
    function ShowErrors() {
        return $this->errors;
    }
}

function ShowSQLError($sql_id,$query="") {
    echo "An error has occured. Report being generated now...<br>";

    echo "Error: ".$sql_id."<br>";
    echo "This is the SQL error:<p>";
    echo mysqli_error()."<p>";
    echo "This is the SQL:<p>";
    echo $query."<br>";

    //echo $data."<br>";
    $db=$GLOBALS['db'];
    //$sql="INSERT INTO error_sql_data (sql_id, output) VALUES ('".EscapeData($sql_id)."','".EscapeData(mysql_error())."')";
    //echo $sql."<br>";
    //$db->query($sql);
    echo "Report generated. Please go back and continue. The problem will be resolved soon.<br>";
    die();
}
?>

警告:mysqli_query()至少需要2个参数,第31行的C:\ xampp \ htdocs \ cyberbot \ classes \ mysql.php中提供1个参数 发生错误。正在生成报告... 错误: 这是SQL错误:

警告:mysqli_error()恰好期望1个参数,第89行的C:\ xampp \ htdocs \ cyberbot \ classes \ mysql.php中给出的参数为0 这是SQL:

INSERT INTO会话(session_id)值('ud6dbiufqhs5tkvu4ev38k1fbj')

注意:未定义的索引:第94行的C:\ xampp \ htdocs \ cyberbot \ classes \ mysql.php中的db 报告已生成。请返回并继续。该问题将很快得到解决。

  

对不起,但是我不知道为什么会这样。 mysqli_query似乎不正确。我尝试添加$ result作为第二个参数,但是没有运气。谢谢

1 个答案:

答案 0 :(得分:0)

mysqli_connect()返回一个代表与数据库的连接的对象。 您需要将此对象作为第一个参数传递给mysqli_query()mysqli_error()函数。

这是您在Connect()函数中命名为$result的变量。