The mysqli_stmt class

(PHP 5, PHP 7, PHP 8)

Introduction

Represents a prepared statement.

Class synopsis

class mysqli_stmt {
/* Properties */
public readonly int|string $affected_rows;
public readonly int|string $insert_id;
public readonly int|string $num_rows;
public readonly int $param_count;
public readonly int $field_count;
public readonly int $errno;
public readonly string $error;
public readonly array $error_list;
public readonly string $sqlstate;
public int $id;
/* Methods */
public __construct(mysqli $mysql, ?string $query = null)
public attr_get(int $attribute): int
public attr_set(int $attribute, int $value): bool
public bind_param(string $types, mixed &$var, mixed &...$vars): bool
public bind_result(mixed &$var, mixed &...$vars): bool
public close(): true
public data_seek(int $offset): void
public execute(?array $params = null): bool
public fetch(): ?bool
public free_result(): void
public more_results(): bool
public next_result(): bool
public num_rows(): int|string
public prepare(string $query): bool
public reset(): bool
public send_long_data(int $param_num, string $data): bool
public store_result(): bool
}

Properties

id

Stores the statement ID.

Table of Contents

add a note add a note

User Contributed Notes 2 notes

up
-7
anonunfinder at gmail dot com
10 years ago
<?php
class MySQL
{
    protected
$mysql;
    function
__construct()
    {
               
//Get MySQL config values from config.ini file
       
if($config = parse_ini_file("../config.ini"))
        {
           
// Obtener los valores del fichero de configuración config.ini
           
$ip = $config["ip"];
           
$user = $config["usuario"];
           
$pass = $config["password"];
           
$bd = $config["bd"];
                         
                        
//Connection between a database and php
           
$this->mysql = new mysqli($ip, $user, $pass, $bd);
        }
    }

    function
setResultQuery($query, $param)
    {
       
$array = NULL;
        if(!
$this->mysql->connect_errno)
        {
           
$stmt = $this->setStatement($query, $param);
            try
            {
                if(
$stmt != NULL)
                {
                    if(
$stmt->execute())
                    {
                       
//Obtener resultados
                       
$stmt->store_result();
                       
$variables = array();
                       
$data = array();
                       
$meta = $stmt->result_metadata();
                        while(
$field = $meta->fetch_field())
                        {
                           
$variables[] = &$data[$field->name];
                        }
                       
call_user_func_array(array($stmt, 'bind_result'), $variables);
                       
$i=0;
                        while(
$stmt->fetch())
                        {
                               
$array[$i] = array();
                                foreach(
$data as $k=>$v)
                               
$array[$i][$k] = $v;
                               
$i++;
                        }
                       
$stmt->close();
                    }
                }
            }catch(
Exception $e){
               
$array = FALSE;
            }
        }
        return
$array;
    }

    function
setStatement($query, $param)
    {
        try
        {
           
$stmt = $this->mysql->prepare($query);
           
$ref = new ReflectionClass('mysqli_stmt');
            if(
count($param) != 0)
            {
               
               
$method = $ref->getMethod('bind_param');
               
$method->invokeArgs($stmt, $param);
            }
        }catch(
Exception $e){
            if(
$stmt != null)
            {
               
$stmt->close();
            }
        }
        return
$stmt;
    }

    function
setNoResultQuery($query, $param)
    {
       
$validation = FALSE;
        if(!
$this->mysql->connect_errno)
        {
            try
            {
               
$stmt = $this->setStatement($query, $param);
                if(
$stmt != null)
                {
                    if(
$stmt->execute())
                    {
                       
$stmt->close();
                       
$validacion = TRUE;
                    }
                }
            }catch(
Exception $e){
               
$validation = FALSE;
            }
        }
        return
$validation;
    }

    function
__destruct()
    {
       
$this->mysql->close();
    }
}
?>
up
-8
krapfi at gmail dot com
15 years ago
The prototype of the mysqli_stmt constructor is mysqli_stmt::__construct(mysqli $link, $query);

To extend mysqli_stmt, do

class myStmt extends mysqli_stmt {
  public function __construct($link, $query) {
    parent::__construct($link, $query);
  }
}

class myI extends mysqli {
  public function prepare($query) {
    return new myStmt($this, $query);
  }
}

http://blog.myhat.de/2007/06/26/pdo-and-extending-mysqli/ has further infos including how to extend mysqli_result
To Top