deprecated php script to convert to msqli pdo please

//connection.php
<?php
//---abstracted ! ------
class connection{
    public $host ="localhost";
    public $user = "root";
    public $password = "";
    public $db="artiste";
    public $dbc;
   
    function __construct() {
        $con = mysqli_connect($this->host, $this->user, $this->password, $this->db);
       
        if(mysqli_errno($con)){
            echo"sum error";
           
        }
        else{
           $this->dbc = $con; // assign $con to $dbc
           echo"connected ";
        }
    }
}
?>

index.php
<?php
require_once('connexion.php');

session_start(); // début de session
$test = new connection();
if (isset($_POST['login'])){ // execution apres envoi du formulaire

$login = mysqli_real_escape_string($test, $_POST['login

?>

i have the following warning
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, object given in C:\wamp\www\kathleen3\php\login\index.php on line 19
any help appreciated
jerrrrryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris Harte2015 Top Expert (Most Article Points)Commented:
You have assigned $dbc to $con, so use it.

$login = mysqli_real_escape_string($test->dbc, $_POST['login']);
0
gr8gonzoConsultantCommented:
Just a suggestion, create classes for different sections of your application, like authentication, and then simply pass them the database connection.

Example:
class Authentication
{
  private $dbc;
  public function __construct($dbc)
  {
    $this->dbc = $dbc;
  }

  public function Login($username,$password)
  {
    // Escape values
    $username = mysqli_real_escape_string($this->dbc, $username);
    $password = mysqli_real_escape_string($this->dbc, $password);

    // Run query
    $result = $this->dbc->query("SELECT field FROM users WHERE username='{$username}' AND password='{$password}');

    ...etc...
  }
}

Then in your startup code:
require_once('libraries/connexion.php');
require_once('libraries/Authentication.php');

...

if (isset($_POST['login']))
{
  $Authentication = new Authentication($test->dbc);
  if($Authentication->Login($_POST["login"], $_POST["password"]))
  {
     // Login successful...
  }
  else
  {
     // Login failed...
  }
}

This way, you isolate the authentication code away from the web page form itself. This makes it easier to maintain the application in the future and extend it in different ways, because you end up building centralized libraries. If you ever need to do a mysqli update in the future, or even change databases entirely, you can just update the libraries instead of trying to search through all of the rest of the code of the system.

The concept is known as MVC, or model/view/controller. This is just a lightweight example, but it gets the point across.
0
jerrrrryAuthor Commented:
then how can ask a query

$sql = "SELECT * FROM membres WHERE login='$login' AND pass='$pass' ";
$res = $test->query($sql);   ??????
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Ray PaseurCommented:
then how can ask a query

That's what this part is about.

 $result = $this->dbc->query("SELECT field FROM users WHERE username='{$username}' AND password='{$password}');

One of the principle advantages of using @gr8gonzo's approach is that you can pass in a production data base or a test data base -- your script does not depend on only one data base.  That is a strategy called "dependency injection" and it makes it possible to test your code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jerrrrryAuthor Commented:
hello i have this;
Fatal error: Using $this when not in object context in C:\wamp\www\kathleen3\php\login\index.php on line 26have i miss something?
0
Ray PaseurCommented:
In PHP, $this is a reserved word, used in class definitions to indicate that the variable is a property of the object.  If you're not familiar with the PHP object model, or oriented programming in general, start your learning adventures here:
http://php.net/manual/en/language.oop5.php

Expect to take some time to learn this stuff.  It's usually a semester or more of college, so don't be impatient with yourself if it's confusing at first.  Once you get the hang of it, you'll realize how we can build virtually bug-free quality software very, very quickly.
0
jerrrrryAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for jerrrrry's comment #a40657214

for the following reason:

no good result
0
gr8gonzoConsultantCommented:
There have been several responses that have all been valid.
1
Ray PaseurCommented:
Agree with @gr8gonzo.  This question has been very well answered!
0
Ray PaseurCommented:
Would like an explanation of the marked down grade.  Please see the grading guidelines here:
http://support.experts-exchange.com/customer/portal/articles/481419
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.