Php mysqli statement error



mysqli_stmt::$errno

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$errno — mysqli_stmt_errno — Returns the error code for the most recent statement call

Description

Returns the error code for the most recently invoked statement function that can succeed or fail.

Parameters

Procedural style only: A mysqli_stmt object returned by mysqli_stmt_init() .

Return Values

An error code value. Zero means no error occurred.

Examples

Example #1 Object-oriented style

/* Open a connection */
$mysqli = new mysqli ( «localhost» , «my_user» , «my_password» , «world» );

/* check connection */
if ( mysqli_connect_errno ()) <
printf ( «Connect failed: %s\n» , mysqli_connect_error ());
exit();
>

$mysqli -> query ( «CREATE TABLE myCountry LIKE Country» );
$mysqli -> query ( «INSERT INTO myCountry SELECT * FROM Country» );

$query = «SELECT Name, Code FROM myCountry ORDER BY Name» ;
if ( $stmt = $mysqli -> prepare ( $query )) <

/* drop table */
$mysqli -> query ( «DROP TABLE myCountry» );

/* execute query */
$stmt -> execute ();

printf ( «Error: %d.\n» , $stmt -> errno );

/* close statement */
$stmt -> close ();
>

/* close connection */
$mysqli -> close ();
?>

Example #2 Procedural style

/* Open a connection */
$link = mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );

/* check connection */
if ( mysqli_connect_errno ()) <
printf ( «Connect failed: %s\n» , mysqli_connect_error ());
exit();
>

mysqli_query ( $link , «CREATE TABLE myCountry LIKE Country» );
mysqli_query ( $link , «INSERT INTO myCountry SELECT * FROM Country» );

$query = «SELECT Name, Code FROM myCountry ORDER BY Name» ;
if ( $stmt = mysqli_prepare ( $link , $query )) <

/* drop table */
mysqli_query ( $link , «DROP TABLE myCountry» );

/* execute query */
mysqli_stmt_execute ( $stmt );

printf ( «Error: %d.\n» , mysqli_stmt_errno ( $stmt ));

/* close statement */
mysqli_stmt_close ( $stmt );
>

/* close connection */
mysqli_close ( $link );
?>

The above examples will output:

See Also

  • mysqli_stmt_error() — Returns a string description for last statement error
  • mysqli_stmt_sqlstate() — Returns SQLSTATE error from previous statement operation

Источник

mysqli_stmt::$sqlstate

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$sqlstate — mysqli_stmt_sqlstate — Returns SQLSTATE error from previous statement operation

Description

Returns a string containing the SQLSTATE error code for the most recently invoked prepared statement function that can succeed or fail. The error code consists of five characters. ‘00000’ means no error. The values are specified by ANSI SQL and ODBC. For a list of possible values, see » http://dev.mysql.com/doc/mysql/en/error-handling.html.

Parameters

Procedural style only: A mysqli_stmt object returned by mysqli_stmt_init() .

Return Values

Returns a string containing the SQLSTATE error code for the last error. The error code consists of five characters. ‘00000’ means no error.

Examples

Example #1 Object-oriented style

/* Open a connection */
$mysqli = new mysqli ( «localhost» , «my_user» , «my_password» , «world» );

/* check connection */
if ( mysqli_connect_errno ()) <
printf ( «Connect failed: %s\n» , mysqli_connect_error ());
exit();
>

$mysqli -> query ( «CREATE TABLE myCountry LIKE Country» );
$mysqli -> query ( «INSERT INTO myCountry SELECT * FROM Country» );

$query = «SELECT Name, Code FROM myCountry ORDER BY Name» ;
if ( $stmt = $mysqli -> prepare ( $query )) <

Читайте также:  Motorola critical error 84 v3i

/* drop table */
$mysqli -> query ( «DROP TABLE myCountry» );

/* execute query */
$stmt -> execute ();

printf ( «Error: %s.\n» , $stmt -> sqlstate );

/* close statement */
$stmt -> close ();
>

/* close connection */
$mysqli -> close ();
?>

Example #2 Procedural style

/* Open a connection */
$link = mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );

/* check connection */
if ( mysqli_connect_errno ()) <
printf ( «Connect failed: %s\n» , mysqli_connect_error ());
exit();
>

mysqli_query ( $link , «CREATE TABLE myCountry LIKE Country» );
mysqli_query ( $link , «INSERT INTO myCountry SELECT * FROM Country» );

$query = «SELECT Name, Code FROM myCountry ORDER BY Name» ;
if ( $stmt = mysqli_prepare ( $link , $query )) <

/* drop table */
mysqli_query ( $link , «DROP TABLE myCountry» );

/* execute query */
mysqli_stmt_execute ( $stmt );

printf ( «Error: %s.\n» , mysqli_stmt_sqlstate ( $stmt ));

/* close statement */
mysqli_stmt_close ( $stmt );
>

/* close connection */
mysqli_close ( $link );
?>

The above examples will output:

Notes

Note that not all MySQL errors are yet mapped to SQLSTATE’s. The value HY000 (general error) is used for unmapped errors.

See Also

  • mysqli_stmt_errno() — Returns the error code for the most recent statement call
  • mysqli_stmt_error() — Returns a string description for last statement error

Источник

mysqli_stmt::$error

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$error — mysqli_stmt_error — Returns a string description for last statement error

Description

Returns a string containing the error message for the most recently invoked statement function that can succeed or fail.

Parameters

Procedural style only: A mysqli_stmt object returned by mysqli_stmt_init() .

Return Values

A string that describes the error. An empty string if no error occurred.

Examples

Example #1 Object-oriented style

/* Open a connection */
$mysqli = new mysqli ( «localhost» , «my_user» , «my_password» , «world» );

/* check connection */
if ( mysqli_connect_errno ()) <
printf ( «Connect failed: %s\n» , mysqli_connect_error ());
exit();
>

$mysqli -> query ( «CREATE TABLE myCountry LIKE Country» );
$mysqli -> query ( «INSERT INTO myCountry SELECT * FROM Country» );

$query = «SELECT Name, Code FROM myCountry ORDER BY Name» ;
if ( $stmt = $mysqli -> prepare ( $query )) <

/* drop table */
$mysqli -> query ( «DROP TABLE myCountry» );

/* execute query */
$stmt -> execute ();

printf ( «Error: %s.\n» , $stmt -> error );

/* close statement */
$stmt -> close ();
>

/* close connection */
$mysqli -> close ();
?>

Example #2 Procedural style

/* Open a connection */
$link = mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );

/* check connection */
if ( mysqli_connect_errno ()) <
printf ( «Connect failed: %s\n» , mysqli_connect_error ());
exit();
>

mysqli_query ( $link , «CREATE TABLE myCountry LIKE Country» );
mysqli_query ( $link , «INSERT INTO myCountry SELECT * FROM Country» );

$query = «SELECT Name, Code FROM myCountry ORDER BY Name» ;
if ( $stmt = mysqli_prepare ( $link , $query )) <

/* drop table */
mysqli_query ( $link , «DROP TABLE myCountry» );

Читайте также:  Git push error cannot lock ref

/* execute query */
mysqli_stmt_execute ( $stmt );

printf ( «Error: %s.\n» , mysqli_stmt_error ( $stmt ));

/* close statement */
mysqli_stmt_close ( $stmt );
>

/* close connection */
mysqli_close ( $link );
?>

The above examples will output:

See Also

  • mysqli_stmt_errno() — Returns the error code for the most recent statement call
  • mysqli_stmt_sqlstate() — Returns SQLSTATE error from previous statement operation

Источник

The mysqli_stmt class

(PHP 5, PHP 7, PHP 8)

Introduction

Represents a prepared statement.

Class synopsis

Properties

Stores the statement ID.

Table of Contents

  • mysqli_stmt::$affected_rows — Returns the total number of rows changed, deleted, inserted, or matched by the last statement executed
  • mysqli_stmt::attr_get — Used to get the current value of a statement attribute
  • mysqli_stmt::attr_set — Used to modify the behavior of a prepared statement
  • mysqli_stmt::bind_param — Binds variables to a prepared statement as parameters
  • mysqli_stmt::bind_result — Binds variables to a prepared statement for result storage
  • mysqli_stmt::close — Closes a prepared statement
  • mysqli_stmt::__construct — Constructs a new mysqli_stmt object
  • mysqli_stmt::data_seek — Seeks to an arbitrary row in statement result set
  • mysqli_stmt::$errno — Returns the error code for the most recent statement call
  • mysqli_stmt::$error_list — Returns a list of errors from the last statement executed
  • mysqli_stmt::$error — Returns a string description for last statement error
  • mysqli_stmt::execute — Executes a prepared statement
  • mysqli_stmt::fetch — Fetch results from a prepared statement into the bound variables
  • mysqli_stmt::$field_count — Returns the number of columns in the given statement
  • mysqli_stmt::free_result — Frees stored result memory for the given statement handle
  • mysqli_stmt::get_result — Gets a result set from a prepared statement as a mysqli_result object
  • mysqli_stmt::get_warnings — Get result of SHOW WARNINGS
  • mysqli_stmt::$insert_id — Get the ID generated from the previous INSERT operation
  • mysqli_stmt::more_results — Check if there are more query results from a multiple query
  • mysqli_stmt::next_result — Reads the next result from a multiple query
  • mysqli_stmt::$num_rows — Returns the number of rows fetched from the server
  • mysqli_stmt::$param_count — Returns the number of parameters for the given statement
  • mysqli_stmt::prepare — Prepares an SQL statement for execution
  • mysqli_stmt::reset — Resets a prepared statement
  • mysqli_stmt::result_metadata — Returns result set metadata from a prepared statement
  • mysqli_stmt::send_long_data — Send data in blocks
  • mysqli_stmt::$sqlstate — Returns SQLSTATE error from previous statement operation
  • mysqli_stmt::store_result — Stores a result set in an internal buffer

User Contributed Notes 2 notes

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

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

Читайте также:  Error 2002 mysql freebsd

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

Источник

mysqli::$sqlstate

(PHP 5, PHP 7, PHP 8)

mysqli::$sqlstate — mysqli_sqlstate — Возвращает код состояния SQLSTATE последней MySQL операции

Описание

Возвращает строку с SQLSTATE кодом последней ошибки. Этот код состоит из пяти символов. ‘00000’ означает отсутствие ошибок. Эти коды определены в стандарте ANSI, а также в ODBC. Посмотреть список возможных значений можно на странице » http://dev.mysql.com/doc/mysql/en/error-handling.html.

Следует заметить, что ещё не все MySQL ошибки имеют отражение в SQLSTATE кодах. Для подобных ошибок используется код HY000 (общая ошибка).

Список параметров

Только для процедурного стиля: объект mysqli , полученный с помощью mysqli_connect() или mysqli_init() .

Возвращаемые значения

Возвращает строку с SQLSTATE кодом последней ошибки. Этот код состоит из пяти символов. ‘00000’ означает отсутствие ошибок.

Примеры

Пример #1 Пример использования $mysqli->sqlstate

= new mysqli ( «localhost» , «my_user» , «my_password» , «world» );

/* проверка соединения */
if ( mysqli_connect_errno ()) <
printf ( «Не удалось подключиться: %s\n» , mysqli_connect_error ());
exit();
>

/* таблица City уже существует, так что мы должны получить ошибку */
if (! $mysqli -> query ( «CREATE TABLE City (ID INT, Name VARCHAR(30))» )) <
printf ( «Ошибка — SQLSTATE %s.\n» , $mysqli -> sqlstate );
>

= mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );

/* проверка соединения */
if ( mysqli_connect_errno ()) <
printf ( «Не удалось подключиться: %s\n» , mysqli_connect_error ());
exit();
>

/* таблица City уже существует, так что мы должны получить ошибку */
if (! mysqli_query ( $link , «CREATE TABLE City (ID INT, Name VARCHAR(30))» )) <
printf ( «Ошибка — SQLSTATE %s.\n» , mysqli_sqlstate ( $link ));
>

Результат выполнения данных примеров:

Смотрите также

  • mysqli_errno() — Возвращает код ошибки последнего вызова функции
  • mysqli_error() — Возвращает строку с описанием последней ошибки

Источник

Оцените статью
toolgir.ru
Adblock
detector