如何使用类将数据库连接到PHP

时间:2015-12-13 06:11:52

标签: php mysql

我创建了db_manager.php文件..即

class db_manager
{
    function DBLogin() 
        {

        $this->connection = mysqli_connect("localhost","root","", "careerknots");

        if(!$this->connection)
        {               
            return false;
        }
        return true;
      }
    }

这是我的upload_resume_action.php文件。

require_once("db_manager.php");
if(isset($_POST['submitted'])) 
{
    $db_manager = new db_manager();
    $con = $db_manager->DBLogin();
    if(!$con)
    {
        echo "Database login failed!";
    }
    $cname = $_POST["uname"];
    $cmob = $_POST["mob"];
    $cemail = $_POST["email"];
    $cfile = $_POST["myfile"];
    $sql = "insert into res_detail(name,mobile,email,resume_loc) values ('$cname','$cmob','$cemail','$cfile')";
    $done = mysqli_query($con,$sql);
    if($done)
    {
        echo "records added successfully";
    }
}

我在第一个参数的mysqli_query中收到错误。据说它希望参数1是mysqli,boolean。

2 个答案:

答案 0 :(得分:1)

  1. db_manager未设置connect属性

  2. 方法DBlogin返回bool。应该返回$this->connection

答案 1 :(得分:-1)

我用$con中的$db_manager->connection中的mysqli_query替换upload_resume_action.php