我想将连接链接传递给一个班级。我尝试以这种方式但无法在此处获得解决方案 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");
?>
和我的类 php 文件
<?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;
}
}
?>
我在直接和通过函数中分配了类似的东西。但没有成功