2017-05-31 22 views
0

嗨iam使用php 7,並被告知它不支持Mysql函數。我研究了網絡,發現fix_mysql.inc.php被告知將所有Myaql函數替換爲Mysqli,但現在我不知道如何實現它。請幫忙!如何使用fix_mysql.inc.php文件替換Mysqli的所有MySql函數

fix_mysql.inc.php

<?php 
/** 
* replacement for all mysql functions 
*/ 
if (!function_exists("mysql_connect")){ 
    /* warning: fatal error "cannot redeclare" if a function was disabled in php.ini with disable_functions: 
    disable_functions =mysql_connect,mysql_pconnect,mysql_select_db,mysql_ping,mysql_query,mysql_fetch_assoc,mysql_num_rows,mysql_fetch_array,mysql_error,mysql_insert_id,mysql_close,mysql_real_escape_string,mysql_data_seek,mysql_result 
    */ 
    function mysql_connect($host, $username, $password){ 
     global $dbconnect; 
     $dbconnect = mysqli_connect($host, $username, $password); 
     return $dbconnect; 
    } 
    function mysql_pconnect($host, $username, $password){ 
     global $dbconnect; 
     $dbconnect = mysqli_connect("p:".$host, $username, $password); 
     return $dbconnect; 
    } 
    function mysql_select_db($db,$dbconnect){ 
     return mysqli_select_db ($dbconnect,$db); 
    } 
    function mysql_ping($dbconnect){ 
     return mysqli_ping ($dbconnect); 
    } 
    function mysql_query($stmt){ 
     global $dbconnect; 
     return mysqli_query ($dbconnect, $stmt); 
    } 
    function mysql_fetch_assoc($erg){ 
     return mysqli_fetch_assoc ($erg); 
    } 
    function mysql_num_rows($e){ 
     return mysqli_num_rows ($e); 
    } 
    function mysql_affected_rows($e=NULL){ 
     return mysqli_affected_rows ($e); 
    } 
    function mysql_fetch_array($e){ 
     return mysqli_fetch_array ($e); 
    } 
    function mysql_error(){ 
     global $dbconnect; 
     return mysqli_error ($dbconnect); 
    } 
    function mysql_insert_id($cnx){ 
     return mysqli_insert_id ($cnx); 
    } 
    function mysql_close(){ 
     return true; 
    } 
    function mysql_real_escape_string($s){ 
     global $dbconnect; 
     return mysqli_real_escape_string($dbconnect,$s); 
    } 
    function mysql_data_seek($re,$row){ 
     return mysqli_data_seek($re,$row); 
    } 
    function mysql_result($res,$row=0,$col=0){ 
     $numrows = mysqli_num_rows($res); 
     if ($numrows && $row <= ($numrows-1) && $row >=0){ 
      mysqli_data_seek($res,$row); 
      $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res); 
      if (isset($resrow[$col])){ 
       return $resrow[$col]; 
      } 
     } 
     return false; 
    } 
    function mysql_get_server_info() { 
     global $dbconnect; 
     return mysqli_get_server_info($dbconnect); 
    } 
    function mysql_set_charset($csname) { 
     global $dbconnect; 
     return mysqli_set_charset($dbconnect,$csname); 
    } 
    function mysql_fetch_object($result) { 
     return mysqli_fetch_object($result); 
    } 
} 
+0

如果」重新開始一個新項目,不要使用這個。請使用[** PDO **](https://secure.php.net/manual/en/book.pdo.php)函數與[** prepared statements **](https://secure.php.net) /manual/en/pdo.prepare.php)和[** bound parameters **](https://secure.php.net/manual/en/pdostatement.bindparam.php)。 –

+0

我已經開始項目,幾乎完成了它。 –

+0

如果你使用這個,你將很容易受到[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻擊。 –

回答

0

我會重寫你的MySQL查詢和使用mysqli的預處理語句,如果我是你。讓我知道你是否想要一些示例代碼來適應自己的使用。

+0

請給我看一個樣本代碼 –

+0

我找到了。 Thanks.https://www.w3schools.com/php/func_mysqli_query.asp –

0

請注意,這只是一個解決方法修復一些舊代碼與此包含函數和結果項目將更容易比如果您使用較新的mysqli函數。如果你真的相信,這是沒有風險設置你的服務器,你可以在你的舊代碼開始加入這一行解決您的舊代碼:

<?php 
include_once('fix_mysql.inc.php'); 

還看到:https://stackoverflow.com/a/37877644/1069083

相關問題