-5

i want to pass connection link to a class. i try in such way but unable to get solution here dbconnect.php

<?php
global $con;
$db_host="localhost";
    $db_username="root";
    $db_pass="";
    $db_name="xxx";

$con= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 

mysqli_select_db($con,$db_name) or die ("no database"); 

?>

and my class php file

<?php
include('dbconnect.php');
$chechout=new clscheckout;


$chechout->con=$con;  // direct assign connection link
$chechout->adult_count=2;
$chechout->child_count=1;
$chechout->child_cwb_count=1;
$chechout->setConn();   // assign throug function
echo $chechout->fnTotel();


class clscheckout{
   public  $pack_id;
   public  $pack_title;
   public  $user_id;
   public  $adult_count;
   public  $child_count;
   public  $child_cwb_count;
   public  $con;


      function setConn ($conn) {
          $con=$conn;
      }

       function fnTotel () {
            $qry='select * from package where pack_id='.$pack_id ;
            $rs=mysqli_query($con,$qry);
            if($rs)
                {
                $row=mysqli_fetch_array($rs);
                $child_wob_price=$row['child_wob_price'];
                $adult_price=$row['adult_price'];
                $child_price=$row['child_price'];
                }

            $adult_total=$adult_price*$adult_count;

            $chidl_total=$child_price*$child_count;
            $child_wob_total=$child_wob_price*$child_cwb_count;

            return $adult_total+$chidl_total+$child_wob_total;

       }

}

?>

I assign the like in both directly and through function. but no get successfull

Ravindr
  • 151
  • 3
  • 13
  • 4
    Please learn to use [**prepared statements**](https://www.youtube.com/watch?v=nLinqtCfhKY). You current code is vulnerable to SQL injections. Also, you should use the object-oriented API for MySQLi, instead of the procedural one. You also might benefit from applying [this](http://stackoverflow.com/a/11369679/727208) approach (adjusted for use with MySQLi instead of PDO) for dealing with DB connection. – tereško Sep 26 '13 at 06:14
  • You don't need to wrap your variables in quotes either, ie `mysqli_connect($db_host, ...` not `mysqli_connect("$db_host", ...` – Phil Sep 26 '13 at 06:18
  • 3
    -1 + CV - A non-computable *"but no get successfull"* with just a weird code-dump and nothing more does not qualify as a programming question. – hakre Sep 26 '13 at 06:20

3 Answers3

1

Firstly, I would assign the connection via your constructor

class clscheckout {

    // snip

    private $con;

    public function __contruct(mysqli $con) {
        $this->con = $con;
    }

    // etc

and instantiate it with

require_once 'dbconnect.php';
$chechout = new clscheckout($con);

Then, make sure you use $this->con in your clscheckout methods instead of $con. This goes for all of your class properties. See http://www.php.net/manual/language.oop5.properties.php

You also don't need global $con in your dbconnect.php script.

Phil
  • 128,310
  • 20
  • 201
  • 202
  • Not only that, there is also `$this->con->query()` instead of `mysqli_query($this->con)` then... . – hakre Sep 26 '13 at 06:19
  • That's the same thing. The procedural MySQLi API just proxies to the OOP methods. I agree though, OOP all the way – Phil Sep 26 '13 at 06:21
  • 2
    With the difference that the object-oriented methods allow injection and stubbing while the procedural functions don't. This can make a huge difference for the code. (maybe I'm a bit far-off with this as I have not tested the latter if the functions are blocking this as I didn't tested but it would feel weird anyway (not per se, just by the testing)). – hakre Sep 26 '13 at 06:22
1

Best way is to first make an instance of mysqli class:

dbconnect.php

$mysqli = new mysqli($host, $user, $pass, $db);

if($mysqli->connect_error) {
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

return $mysqli;

then

$connection = require_once('dbconnect.php');
$chechout = new clscheckout($connection);

and manipulate/pass it as you wish. That way you won't use nasty globals

class clscheckout{
   public  $con;


      public function __construct ($conn) {
          $this->con=$conn;
      }

Then use it as an object

$qry='select * from package where pack_id='.$pack_id ;
$rs=$this->con->query($qry);
Sergey Telshevsky
  • 11,613
  • 6
  • 51
  • 77
0

Did you tried this (passing the $conn by reference rather than by value) ?

function setConn (&$conn) {
      $con=$conn;
}

See http://php.net/manual/en/language.references.php for more informations.

cubitouch
  • 1,839
  • 13
  • 27