-2

Alright. So basically. Here's my code.

login.php

require_once("./libs/db_cxn.php");

    $cxn = new newConnection();

    $stmt = "SELECT users.username FROM users WHERE username=$username AND password=MD5($password)";
    $qry = \mysqli_prepare($cxn, $stmt);
    $res = mysqli_execute($qry);

db_cxn.php

class newConnection{

    function __construct(){
        $conf = array(
            "host" => "localhost",
            "username" => "root", 
            "password" => "root",
            "dbname" => "db_BookWorm",
            "port" => ini_get("mysqli.default_port"),
            "socket" => ini_get("mysqli.default_socket"),
            "prefix" => "bWorm_"
        );
        $cxn = new mysqli($conf["host"], $conf["username"], $conf["password"], $conf["dbname"]);

        if ($cxn->connect_errno){
            \printf("Connection failed: %s\n", $cxn->connect_error);
            exit();
        }
    }

    function _destruct(){
        mysqli_close($this);
    }
}

I have error reporting on, and the following error message is given whenever i attempt to run that code

Warning: mysqli_prepare() expects parameter 1 to be mysqli, object given in \\MORIARTY\HOME\15\5CBA2901\Desktop\comp_proj\project\www\login.php on line 34

Any clues on how to fix this? I have created this connection class due to how frequent i will be opening connections, and figured it'd save time and grief on my part. Any help would be appreciated!

cbartlett
  • 41
  • 1
  • 1
  • 3
  • The `$cxn` that you pass to `mysqli_prepare` is an object instance of type `newConnection`. But `mysqli_prepare` needs an object instance of type `mysqli`. What you're doing is simply not how objects work. – deceze Nov 11 '14 at 11:59

1 Answers1

0

Change your code like that:

<?php

/**
  ---------------------
  for db_cxn.php file
  ---------------------
 * */
class newConnection {
    static $cxn;

    function __construct() {
        $conf = array(
            "host" => "localhost",
            "username" => "root",
            "password" => "",
            "dbname" => "test",
            "port" => ini_get("mysqli.default_port"),
            "socket" => ini_get("mysqli.default_socket"),
            "prefix" => ""
        );
        self::$cxn = mysqli_connect($conf["host"], $conf["username"], $conf["password"], $conf["dbname"]);

        if (self::$cxn->connect_errno) {
            \printf("Connection failed: %s\n", self::$cxn->connect_error);
            exit();
        }
    }

    function getConnection() {
        return self::$cxn;
    }

    function _destruct() {
        mysqli_close($this);
    }
}
<?php

/* * *
  ----------------
  for login.php
  ----------------
 */
require_once("./libs/db_cxn.php");

$obj = new newConnection();
$cxn = $obj->getConnection();
$id = 1;
$stmt = "SELECT users.username FROM users WHERE username=$username AND password=MD5($password)";
$qry = mysqli_prepare($cxn, $stmt);
$res = mysqli_execute($qry);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Satender K
  • 571
  • 3
  • 13