php GetSQLValueString替代mysqli

hyrbngr7  于 2023-04-19  发布在  PHP
关注(0)|答案(3)|浏览(136)

我试图更新到mysqli,我几乎已经更新了整个网站,但我面临着一个问题与GetSQLValueString。我如何改变这与mysqli兼容或如果必要的删除/更改。

<?php
if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", 
$theNotDefinedValue = "") 
    {
        if (PHP_VERSION < 6) {
            $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
        }

        $theValue = function_exists("mysql_real_escape_string") ?
            mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

        switch ($theType) {
            case "text":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "long":
            case "int":
                $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                break;
            case "double":
                $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                break;
            case "date":
                $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                break;
            case "defined":
                $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                break;
        }
        return $theValue;
    }
}

$colname_Recordset1 = "-1";
if (isset($_GET['stid'])) {
    $colname_Recordset1 = $_GET['stid'];
}
mysqli_select_db( $SIS,$database_SIS);
$query_Recordset1 = sprintf("SELECT * FROM payments WHERE stid = %s", 
GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysqli_query($SIS, $query_Recordset1) or die(mysqli_error());
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysqli_num_rows($Recordset1);

?>
alen0pnh

alen0pnh1#

你需要将所有的mysql_改为mysqli_,并添加到数据库的连接作为第一个参数。在我的例子中,到数据库的连接是**$conn_vote**。请注意,我添加了$conn_vote作为函数的参数:

function GetSQLValueString($conn_vote, $theValue, $theType,   $theDefinedValue = "", $theNotDefinedValue = "")   
    {  
      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) :   $theValue;  

      $theValue = function_exists("mysqli_real_escape_string") ?   mysqli_real_escape_string($conn_vote, $theValue) :  
mysqli_escape_string($conn_vote, $theValue);

      switch ($theType) {  
        case "text":  
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";  
          break;      
        case "long":  
        case "int":  
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";    
          break;    
        case "double":    
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";  
          break;  
        case "date":  
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;  
        case "defined":  
          $theValue = ($theValue != "") ? $theDefinedValue :
$theNotDefinedValue;
break;
}
return $theValue;
}
}
hc8w905p

hc8w905p2#

这为我工作'$config'作为dbase连接!

if (!function exists("GetSQLValueString")) {
function GetSQLValueString($config,$theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = ""){
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($config,$theValue) : mysqli_escape_string($conFig,$theValue);
  switch ($theType) {
case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

sprintf(INSERT或Update)使用:GetSQLValueString($config,$_POST['fname'],“text”)

p3rjfoxz

p3rjfoxz3#

我也面临着同样的问题,我发现更合适的方法是摆脱这个函数,使用mysqli_real_escape_string,你可以像这样传递数据:

mysqli_real_escape_string($GLOBALS["___mysqli_ston"], md5($password))

相关问题