WEB언어/PHP

PHP7에서 mysql_connect 를 mysqli_connect 로 사용

saltdoll 2018. 12. 5. 04:58
반응형

MySQLi 사용하기

PHP7에서 mysql_connect를 사용할 수 없어서 변경을 해줘야 합니다.

PHP7환경에서 mysql_connect() 를 사용하려면, mysqli_connect(), PDO::__construct()를 사용해야 합니다.

 

 

 

추가로 변경해야 하는 것들

 

For instance:

  • with mysql, you have to use the mysql_select_db once connected, to indicate on which database you want to do your queries
  • mysqli, on the other side, allows you to specify that database name as the fourth parameter to mysqli_connect.
  • Still, there is also a mysqli_select_db function that you can use, if you prefer.

 

 

 

참고: https://stackoverflow.com/questions/1390607/how-to-change-mysql-to-mysqli
참고2: https://stackoverflow.com/questions/34088373/call-to-undefined-function-mysql-connect-after-upgrade-to-php-7 
참고3: http://php.net/manual/en/mysqli.select-db.php 
참고4: http://php.net/manual/en/mysqli.query.php

 

[Others]

DB Class's

class Database {
  /*This is a modular class that masks the underlying MySQL calls and provides a generic interface to its client. */
  function __construct($host, $port, $dbname, $user, $password, $transactional = true) {
    /* Ignore SSL since MySQL does not support it */
    if ($port) {
      $host = "$host:$port";
    }
    $this->Connection = new MySQLi($host, $user, $password, $dbname);
    if ($transactional){
      $this->Query("SET AUTOCOMMIT=0");
    }
  }

  function Query($sql){
    $this->Result = $this->Connection->query($sql);
    if ($this->Result === false){
      $this->HandleError($sql);
    }
  }

  function Select($sql){
    $this->Query($sql);
  }

  function Update($sql){
    /* Right now this does the same thing as select...but it leaves room for expansion with replication */
    $this->Query($sql);
  }

  function Next($associative = True){
    /* Return the next row in the result set */
    if($associative){
      return $this->Result->fetch_assoc();
    }
    else {
      return $this->Result->fetch_array();
    }
  }

  function ResultLength(){
    return $this->Result->num_rows;
  }

  function HandleError($sql) {
    /* This function should be overridden to provide custom error handling for the application */
    // die(ErrorMessage("mysql", "MySQL Error in $sql: " . mysql_error($this->Connection))); NOT SURE WHAT TO DO WITH THIS
  }

  function Commit(){
    $this->Query("COMMIT");
  }

  function Rollback(){
    $this->Query("ROLLBACK");
  }
}

 

 

 

 

include("shared.inc.php");
class StoredProcedure {
  /*This is a modular class that provides a generic interface for calling stored procedures or the equivalent */
  function StoredProcedure($name, $update, $parameters, $defaults){
    $this->name = $name;
    $this->update = $update;
    $this->parameters = $parameters;
    $this->defaults = $defaults;
  }

  function GetQuery($arguments){
    global $StoredProcedures;
    $callparameters = array();
    foreach($this->parameters as $parameter){
      if(isset($arguments[$parameter])){
        $callparameters[] = $this->Escape($arguments[$parameter]);
      }
      else if (isset($this->defaults[$parameter])){
        $callparameters[] = $this->Escape(DefaultParameter($this->defaults[$parameter]));
      }
      else {
        die(MissingParameterError($parameter));
      }
    }
    return vsprintf($StoredProcedures[$this->name], $callparameters);
  }
  
  function ReturnsRows() { //Does this stored procedure return a recordset?
    return ($this->update == false);
  }

  function Escape($value){
    if ($value === false){
      return "NULL";
    }
    return "'" . mysql_escape_string($value) . "'";
  }

  function Call(&$database, $arguments){
    if ($this->update){
      $database->Update($this->GetQuery($arguments));
    }
    else {
      $database->Select($this->GetQuery($arguments));
    }
  }
}

참고: https://www.experts-exchange.com/questions/28935887/mysql-connect-PHP-7.html

 

 



반응형
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)