Подготовленный запрос PDO написать юзая MYSQLI

Пэлт

всем привет!;)
я ещё думаю что выбрать для работы с БД

PHP:
  1.         $sql = «SELECT * FROM pidors WHERE login = ? AND password = ? LIMIT 1»;
  2.         $stmt = $pdo->prepare($sql);
  3.         $stmt->execute([$login, $password]);
  4.         $data = $stmt->fetchAll();

Напишите, пожалуйста эти строки кода, как бы это делалось без PDO.
Я про подготовленные запросы в бд на мускли.

 

Drunkenmunky

lastdays

Орнул с названия таблицы))
p.s. так вот, как ты о своих клиентах :D

 

MouseZver

Знайте, вы пидоры !!!
— Добавлено —

PHP:
  1. $stmt = $db -> prepare( ‘SELECT * FROM table WHERE column1 = :user’, [ ‘user’ => ‘pidor’ ] );
  2.  
  3. echo ( $stmt -> rowCount() ? ‘есть пидор’ : ‘нету пидора’ );

— Добавлено —

а ну ок.

PHP:
  1. <?php
  2.  
  3. declare ( strict_types = 1 );
  4.  
  5. /*
  6.     @ Author: MouseZver
  7.     @ Email: mouse-zver@xaker.ru
  8.     @ url-source: http://github.com/MouseZver/Lerma
  9.     @ php-version 7.4
  10. */
  11.  
  12. namespace NouvuDatabaseLermaExt;
  13.  
  14. use NouvuDatabase{
  15.     Lerma,
  16.     InterfaceDriver
  17. };
  18. use Error;
  19. use NouvuConfigConfig;
  20.  
  21. final class Mysql implements InterfaceDriver
  22. {
  23.     private $statement = null;
  24.    
  25.     private $query = null;
  26.    
  27.     private $result;
  28.    
  29.     private Lerma $lerma;
  30.    
  31.     private Config $config;
  32.    
  33.     private string $driver;
  34.    
  35.     private mysqli $connect;
  36.    
  37.     private array $params;
  38.    
  39.     public function __construct ( Lerma $lerma, Config $config, string $driver )
  40.     {
  41.         $this -> lerma = $lerma;
  42.        
  43.         $this -> config = $config;
  44.        
  45.         $this -> driver = $driver;
  46.        
  47.         $this -> params = $this -> config -> get( «drivers.{$this -> driver}» );
  48.        
  49.         mysqli_report ( MYSQLI_REPORT_STRICT );
  50.        
  51.         $this -> connect();
  52.     }
  53.    
  54.     private function connect()
  55.     {
  56.         try
  57.         {
  58.             $this -> connect = new mysqli(
  59.                 $this -> params[‘host’],
  60.                 $this -> params[‘username’],
  61.                 $this -> params[‘password’],
  62.                 $this -> params[‘dbname’],
  63.                 ( int ) $this -> params[‘port’]
  64.             );
  65.            
  66.             if ( $this -> connect -> connect_error )
  67.             {
  68.                 throw new Error( sprintf ( $this -> config -> get( «errMessage.connect.{$this -> driver}» ), $this -> connect -> connect_errno, $this -> connect -> connect_error ) );
  69.             }
  70.            
  71.             $this -> connect -> set_charset( $this -> params[‘charset’] );
  72.         }
  73.         catch ( mysqli_sql_exception $e )
  74.         {
  75.             $this -> config -> get( «ShemaExceptionConnect.{$this -> driver}» )( $e );
  76.         }
  77.     }
  78.    
  79.     public function query( string $sql ): void
  80.     {
  81.         $this -> connect -> ping() ?: $this -> connect();
  82.        
  83.         $this -> query = $this -> connect -> query( $sql );
  84.     }
  85.    
  86.     public function prepare( string $sql ): void
  87.     {
  88.         $this -> connect -> ping() ?: $this -> connect();
  89.        
  90.         $this -> statement = $this -> connect -> prepare( $sql );
  91.     }
  92.    
  93.     public function fetch( int $int )
  94.     {
  95.         switch ( $int )
  96.         {
  97.             case Lerma :: FETCH_NUM:
  98.             {
  99.                 return $this -> result() -> fetch_array( MYSQLI_NUM );
  100.             }
  101.            
  102.             case Lerma :: FETCH_ASSOC:
  103.             {
  104.                 return $this -> result() -> fetch_array( MYSQLI_ASSOC );
  105.             }
  106.            
  107.             case Lerma :: FETCH_OBJ:
  108.             {
  109.                 return $this -> result() -> fetch_object();
  110.             }
  111.            
  112.             case Lerma :: MYSQL_FETCH_BIND:
  113.             {
  114.                 return $this -> statement -> fetch();
  115.             }
  116.            
  117.             case Lerma :: MYSQL_FETCH_FIELD:
  118.             {
  119.                 return ( array ) $this -> result() -> fetch_field();
  120.             }
  121.            
  122.             default:
  123.             {
  124.                 return null;
  125.             }
  126.         }
  127.     }
  128.    
  129.     public function fetchAll( int $int )
  130.     {
  131.         switch ( $int )
  132.         {
  133.             case Lerma :: FETCH_NUM:
  134.             {
  135.                 return $this -> result() -> fetch_all( MYSQLI_NUM );
  136.                
  137.                 break;
  138.             }
  139.            
  140.             case Lerma :: FETCH_ASSOC:
  141.             {
  142.                 return $this -> result() -> fetch_all( MYSQLI_ASSOC );
  143.            
  144.                 break;
  145.             }
  146.            
  147.             case Lerma :: MYSQL_FETCH_FIELD:
  148.             {
  149.                 return $thiss -> result() -> fetch_fields();
  150.            
  151.                 break;
  152.             }
  153.            
  154.             default:
  155.             {
  156.                 return null;
  157.             }
  158.         }
  159.     }
  160.    
  161.     public function columnCount(): int
  162.     {
  163.         return $this -> connect -> field_count;
  164.     }
  165.    
  166.     public function rowCount(): int
  167.     {
  168.         return $this -> result() -> num_rows;
  169.     }
  170.    
  171.     public function InsertID(): int
  172.     {
  173.         return $this -> connect -> insert_id;
  174.     }
  175.    
  176.     public function rollBack( $rollback ): bool
  177.     {
  178.         return $this -> connect -> rollback( $rollback );
  179.     }
  180.    
  181.     public function beginTransaction( $rollback ): bool
  182.     {
  183.         return $this -> connect -> begin_transaction( $rollback );
  184.     }
  185.    
  186.     public function commit( $commit ): bool
  187.     {
  188.         return $this -> connect -> commit( $commit );
  189.     }
  190.    
  191.     public function isError(): void
  192.     {
  193.         $obj = $this -> statement ?: $this -> connect;
  194.        
  195.         if ( $obj -> errno )
  196.         {
  197.             throw new Error( $obj -> error );
  198.         }
  199.     }
  200.    
  201.     public function binding( array $binding ): void
  202.     {
  203.         $this -> result = null;
  204.        
  205.         $for = [ » ];
  206.        
  207.         $count = 0;
  208.        
  209.         foreach ( $this -> lerma -> executeHolders( $binding[0] ) AS $args )
  210.         {
  211.             $short = [
  212.                 ‘integer’ => ‘i’,
  213.                 ‘double’ => ‘d’,
  214.                 ‘string’ => ‘s’,
  215.                 ‘NULL’ => ‘s’
  216.             ];
  217.            
  218.             $type = gettype ( $args );
  219.            
  220.             if ( ! isset ( $short[$type] ) )
  221.             {
  222.                 throw new Error( «Invalid type {$type}« );
  223.             }
  224.            
  225.             $for[0] .= $short[$type];
  226.            
  227.             $count++;
  228.         }
  229.        
  230.         for ( $i = 0; $i < $count; $for[] = &${ ‘bind_’ . $i++ } ){}
  231.        
  232.         $this -> statement -> bind_param( $for );
  233.  
  234.         foreach ( $binding AS $items )
  235.         {
  236.             $items = $this -> lerma -> executeHolders( $items );
  237.            
  238.             extract ( $items, EXTR_PREFIX_ALL, ‘bind’ );
  239.            
  240.             $this -> statement -> execute();
  241.         }
  242.     }
  243.    
  244.     public function bindResult( $result )
  245.     {
  246.         if ( is_null ( $this -> result ) )
  247.         {
  248.             return $this -> statement -> bind_result( $result );
  249.         }
  250.        
  251.         throw new Error( $this -> config -> get( ‘errMessage.statement.bindResult’ ) );
  252.     }
  253.    
  254.     public function close(): InterfaceDriver
  255.     {
  256.         if ( gettype ( $close = ( $this -> statement ?? $this -> query ) ) != ‘boolean’ && ! is_null ( $close ) )
  257.         {
  258.             $close -> close();
  259.         }
  260.        
  261.         $this -> statement = $this -> query = $this -> result = null;
  262.        
  263.         return $this;
  264.     }
  265.    
  266.     /*
  267.         — Определение типа запроса в базу данных
  268.     */
  269.     protected function result()
  270.     {
  271.         if ( ! is_null ( $this -> statement ) )
  272.         {
  273.             return $this -> result ?: $this -> result = $this -> statement -> get_result();
  274.         }
  275.        
  276.         return $this -> query;
  277.     }
  278. }
 

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *