简体   繁体   English

在php类中打开mysql连接时出现问题

[英]Problem opening mysql connection in php class

I am trying to create my own class to handle mysql interactions on a test site I'm making. 我正在尝试创建自己的类来处理我正在创建的测试站点上的mysql交互。 I can't get the mysqli_connect() to work within the class declaration. 我无法在类声明中使用mysqli_connect()。 Every time I try to use the class in a script I get this syntax error: 每次尝试在脚本中使用该类时,都会出现以下语法错误:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\\xampp\\htdocs\\autoshop\\classes\\class_db.inc.php on line 7 解析错误:语法错误,第7行的C:\\ xampp \\ htdocs \\ autoshop \\ classes \\ class_db.inc.php中出现意外的'(',期望的','或';'

Here is the code I have so far: 这是我到目前为止的代码:

<?php 
class Db{
    private $host = 'localhost';
    private $db_user = 'username';
    private $db_pass = 'password';
    private $db_name = 'dbname';
    private $con = mysqli_connect($this->host, $this->db_user, $this->db_pass, $this->db_name) or die('failed to connect');

    public function valid_login($login_user, $login_pass){

        if(isset($login_user) && isset($login_pass) && ($login_user !== "") && ($login_pass !== "")){
            include('./includes/functions.php');

            $clean_lu = mysqli_real_escape_string($this->con, $login_user);
            $clean_lp = mysqli_real_escape_string($this->con, $login_pass);

            $login_query = "SELECT hash FROM users WHERE user_name='$clean_lu'";
            $login_results = mysqli_query($this->con, $login_query) or die('bad query');
            $login_hash = mysqli_fetch_array($login_results);

            if(isset($login_hash[0])){
                if(check_hash($clean_lp, $login_hash[0])){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
    }

    mysqli_close($this->con);   
}
?>

I keep looking at that line and I can't find any bad syntax. 我一直在看那行,找不到任何不好的语法。

I apologize if this is a really simple solution that's right in front of my face, I'm pretty new at this whole developer thing. 如果这是一个摆在我面前的非常简单的解决方案,我深表歉意,对于整个开发人员来说,我还是一个新手。

You cannot initialize object's member with result of function call. 您不能使用函数调用的结果初始化对象的成员。 Perform this in your constructor instead. 而是在构造函数中执行此操作。

class Db
{
    private $con;

    public function __construct()
    {
        $this->con = mysqli_connect($this->host, $this->db_user, $this->db_pass, $this->db_name) or die('failed to connect');
    }
    ...
}

Also, where is mysqli_close($this->con); 另外, mysqli_close($this->con);在哪里mysqli_close($this->con); located? 位于? It doesn't belong to any of methods. 它不属于任何方法。

Btw, any reason to close connection manually at all? 顺便说一句,有什么理由要手动关闭连接吗?

maybe try this 也许试试这个

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) 
{
trigger_error('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM