1

Estou tentando converter uma função para Mysqli, para usar com o PHP7.1. Estou tendo dificuldades com mysqli_real_escape_string, e mysqli_escape_string.

Erro:

Notice: Undefined variable: mysqli in config.php on line 16

Warning: mysqli_escape_string() expects parameter 1 to be mysqli, null given in > config.php on line 16

ARQUIVO CONFIG.PHP

require_once('Connections/conexao.php');

$mysqli = new mysqli($hostname_conexao,$username_conexao,$password_conexao,$database_conexao); if($mysqli->connect_error){echo "Erro";exit();}

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") ? mysqli_real_escape_string($mysqli, $theValue) : mysqli_escape_string($mysqli, $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;
    }
}

O que preciso fazer?

2 Respostas 2

1

O problema é que a função GetSQLValueString está tentando acessar uma variável que está fora dela, ou seja a variavel esta em um escopo diferente, para ajustar use o global assim:

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{

     global $mysqli;

Você também pode tentar outras abordagens como OOP, mas isto é outra história, é apenas uma sugestão.

1

Troque o mysql por mysqli, acredito que seja um erro ortográfico seu.

Ficaria assim:

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

No modo objeto orientado, ele pede somente um parâmetro, ficaria assim:

$theValue = function_exists("mysqli_real_escape_string") ? mysqli->real_escape_string($theValue) : mysqli->escape_string($theValue);
4
  • Vi a falha na escrita, mas continua o erro.
    – Tiago
    15/06/2017 às 17:09
  • Antes de executar a função, cheque o valor de $theValue.
    – Francisco
    15/06/2017 às 17:14
  • Eu uso a função no momento do cadastro e atualização da tabela assim GetSQLValueString($_POST['kg'], "text"),.
    – Tiago
    15/06/2017 às 17:15
  • Editei minha resposta, veja se funciona agora.
    – Francisco
    15/06/2017 às 17:22

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .