Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. /* parte 1 */
  2.         public function OpenConnection(){
  3.        
  4.             $connection = mysqli_connect(Database::_HOST, Database::_DB_USER, Database::_DB_PASSWORD, Database::_DB);
  5.            
  6.             mysqli_set_charset($connection,"utf8");
  7.            
  8.             if(!$connection){
  9.                 echo "<p font color=\"red\">Erro: Não foi possível conectar com o banco de dados." . PHP_EOL . "</p>";
  10.                 echo "<p font color=\"red\">Erro: " . mysqli_connect_errno() . PHP_EOL . "</p>";
  11.                 echo "<p font color=\"red\">Erro: " . mysqli_connect_error() . PHP_EOL . "</p>";
  12.             }
  13.             return $connection;
  14.         }//DBOpenConnection's end
  15.  
  16. /* parte 2 */
  17.  
  18.         $connection = Database::getInstance()->OpenConnection();
  19.             $sql = "select * from user where user_name = ? and password = ?;";
  20.             $statement = $connection->prepare($sql);
  21.                
  22.             //if any error happen in connection
  23.             if (!$statement) {
  24.                 echo 'erro na consulta: '. $connection->errno .' - '. $connection->error;
  25.             }
  26.                
  27.             //associate the user name and password with sql parameters,
  28.             //execute the query and store the result
  29.             $statement->bind_param('ss', $userName,$password);
  30.             $statement->execute();
  31.             $result = $statement->get_result();
  32.                
  33.             $user;
  34.            
  35.             //create and set the user variable
  36.             while($row = mysqli_fetch_array($result)){
  37.                
  38.                 $user = new User($row["idUser"], $row["name"], $row["email"], $row["user_name"], $row["password"], $row["type"], $row["active"], $row["idAdministrator"]);
  39.             }
  40.            
  41.             echo "teste: ".$user->name;
  42.    
  43.             //close statement and connection
  44.             $statement->close();
  45.             Database::getInstance()->CloseConnection($connection);
  46.            
  47.             return $user; //aqui ta retornando nulo...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement