简体   繁体   中英

MYSQL ERROR not showing correctly in Class Function

$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'
    }

here $conn->error; shows correct result but in below class its not showing anything

But Problem is

    <?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');

Here in error it's not showing anything, just blank; where problem can anyone help me?

The connect() method in your conn class creates a new connection every time you call it. In your insert_user() method you call connect() to run the query, then call it again to find the error. The new connection you're using to find the error is separate from the connection used to run the query, so there's no error to be found.

Additionally, creating a new connection for every new query without closing them is likely to exhaust the connection limit. You should create one connection and re-use that every time.

Below is a revised version of your code. Notes:

  • MySQLi is set to throw exceptions if it encounters an error. These exceptions are trapped and reported in the try...catch block.
  • I've changed your code to use a prepared statement to avoid SQL injection.
  • Your database parameters are passed into the Work constructor
    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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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