Fatal error: Uncaught ArgumentCountError: mysqli_real_escape_string() expects exactly 2 arguments, 1

Hi All, I’m trying to get a php script to work in php8.0 (it was working in php5.6)
One of tha last errors is Fatal error: Uncaught ArgumentCountError: mysqli_real_escape_string() expects exactly 2 arguments, 1

It comes from if (isset($_GET['name'])) $qname="AND u.name LIKE '%".mysqli_real_escape_string ($_GET['name'])."%'";

I already tried ($this->link, $_GET['name']) but that gives the error that you can’t use $this on that place.
Who can help me?
Thanks in advance.

Look at the free manual for that function and it will show you exactly what is needed.

If I had found the answer in the Manuel I didn’t ask it here, but thanks for your response.

It’s right on the page under “Parameters” and tells you what the two arguments the function expects.

Under procedural examples it shows a code example with the two arguments.

https://www.php.net/manual/en/mysqli.real-escape-string.php

My bad I was inspecting help on phphelp not a a link to the manual.
You can remove this topic I will try to find answers elsewhere.

You got help and were told exactly where the answer is along with a code sample. If you expect to be spoon feed answers your not going to make it very far as a programmer.

That answer is not working for me, therefor I ask help here.
I and just want to edit one php script and have no intention to become a php programmer.

As soon as you open a php file with a programming editor and type a single character, you ARE a php programmer, responsible for what is in that file and what the meaning is of the characters you are typing, no matter how capable or how uninterested you are in the subject :grin:

The mysqli real_escape_string function (and several more primary mysqli statements) requires the mysqli connection as a parameter. Your task would be to identify what php variable is holding the msyqli connection and use it where it is needed. Since we don’t know what that variable is, and since this is a fairly obvious task, we cannot help you beyond pointing you in the direction of the documentation that defines the requirements for the function you are trying to make work.

I already tried ($this->link, $_GET['name']) but that gives the error that you can’t use $this on that place.

$this->link is the connection variable
from ` $this->link = mysqli_connect($this->host, $this->user, $this->pass, $this->db);

I also tried $db->link, $_GET[‘name’]) but that also don’t work.`

That’s an OOP class property. Accessing it inside of an instance of its class would use $this->link. To access it outside of an instance of its class, which is where the mysqli_real_escape_string call is at, would require knowing what variable is holding the instance of that class.

Also, these type of database class ‘wrappers’ are usually just a bunch of code for nothing, that don’t help simplify writing applications.

At this point, it would take having all the relevant code in order to help you. It may be that the amount of changes, beyond fixing the current error, to make this work, is much more than to simply rewrite the database code using best practices.

BTW - using mysqli_real_escape_string only provides protection for string data types and only if you have set the character set that php is using to match your database tables (this point is mentioned in the ‘caution’ statement in the mysqli_real_escape_string documentation.) It provides no protection for numerical data types. The best method for providing protection for all data types is to use prepared queries, which actually simplifies converting old code, because it eliminates the need to use escape string functions and it simplifies the sql query syntax. Unfortunately, prepared queries using the mysqli extension are overly complicated and require you to essentially learn two different sets of statements. It is better and much simpler to convert code to use the PDO database extension instead.

Ok thanks, than I stop with it and try to find a other solution, thanks for your answer.

Sponsor our Newsletter | Privacy Policy | Terms of Service