首页 > 解决方案 > MYSQL 错误在类函数中未正确显示

问题描述

$conn = new mysqli($host,$user,$pass,$dbname);    
$sql = "INSERT INTO user (email,password) VALUES ('user@example.com','asdfasdfasdfsadf')";
    if($conn->query($sql) != true){
        echo $conn->error;
        // Its display correct result, Duplicate entry 'user@example.com' for key 'email'
    }

这里 $conn->error; 显示正确的结果,但在下面的课程中它没有显示任何内容

但问题是

    <?php
class db
{
  private $host;
  private $user;
  private $pass;
  private $dbname;
}
class conn extends db
{
  public function connect()
  {
    $this->host = "localhost";
    $this->user = "root";
    $this->pass = "";
    $this->dbname = "jobportal";
    $conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
    return $conn;
  }
}


class work extends conn
{

 public function insert_user($email, $password_hash)
  {
    $sql = "INSERT INTO user (email,password) VALUES ('user@example.com','asdfasdf')";
    if ($this->connect()->query($sql) == TRUE) {
      echo 'inserted';
    } else {
      return $this->connect()->error;
      // Return Blank No Result it returns, As its duplicated value it it will insert , so it will show error as duplicate.
    }
  }
}

$obj = new work;
echo $obj->insert_user('user@example.com', 'asdfasdf');

在这里,它没有显示任何内容,只是空白;有什么问题可以帮我吗?

标签: phpmysql

解决方案


每次调用时,类中的connect()方法都会创建一个新连接。conn在您insert_user()调用的方法connect()中运行查询,然后再次调用它以查找错误。您用于查找错误的新连接与用于运行查询的连接是分开的,因此不会发现任何错误。

此外,为每个新查询创建一个新连接而不关闭它们可能会耗尽连接限制。您应该创建一个连接并每次都重新使用它。

以下是您的代码的修订版本。笔记:

  • MySQLi 设置为在遇到错误时抛出异常。这些异常被捕获并报告在try...catch块中。
  • 我已更改您的代码以使用准备好的语句来避免 SQL 注入。
  • 您的数据库参数被传递到Work构造函数
    class Conn           // No parent db class - it serves no purpose
                         // Class names begin with capitals, by convention
    {
    
        private $host;   // Local copies of database credentials. Not really needed
        private $user;
        private $pass;
        private $dbname;
    
        // Set up the database connection in the constructor.
        public function __construct(string $host, string $user, string $password, string $dbname) {
            $this->host = $host;
            $this->user = $user;
            $this->pass = $password;
            $this->dbname = $dbname;
    
            // Set mysqli to throw exceptions on error
            mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

            // open a connection for later use
            $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
        }
    }
    
    class Work extends Conn
    {
    
        public function insert_user(string $email, string $password_hash)
        {
            $sql = "INSERT INTO user (email,password) VALUES (?,?)";
            $stmt = $this->conn->prepare($sql);
            $stmt->bind_param('ss', $email, $password_hash);
            $stmt->execute();
            return;
        }
    }
    
    
    
    try {
        $obj = new Work("server", "username", "password", "schema");
        $obj->insert_user('user@example.com', 'asdfasdf');
        echo "inserted";
    } catch (Exception $e) {
        echo $e->getMessage();
        }


推荐阅读