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?
 
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
 
Chris HarteThaumaturgeCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jerrrrryAuthor Commented:
then how can ask a query

$sql = "SELECT * FROM membres WHERE login='$login' AND pass='$pass' ";
$res = $test->query($sql);   ??????
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.