MySQLi Function in PHP: Part 12

In this article I describe the PHP MySQLI functions mysqli_sqlstate, mysqli_ssl_set, mysqli_stat, mysqli_stmt_init, mysqli_thread_id and mysqli_thread_safe.

Introduction

In this article I describe the PHP MySQLI functions mysqli_sqlstate, mysqli_ssl_set, mysqli_stat, mysqli_stmt_init, mysqli_thread_id and mysqli_thread_safe. To learn some other MySQLI functions, go to:

  1. MySQLi Function in PHP: Part 1
  2. MySQLi Function in PHP: Part 2
  3. MySQLi Function in PHP: Part 3
  4. MySQLi Function in PHP: Part 4
  5. MySQLi Function in PHP: Part 5
  6. MySQLi Function in PHP: Part 6
  7. MySQLi Function in PHP: Part 7
  8. MySQLi Function in PHP: Part 8
  9. MySQLi Function in PHP: Part 9
  10. MySQLi Function in PHP: Part 10
  11. MySQLi Function in PHP: Part 11
PHP mysqli_sqlstate() Function

The PHP MySQLi "mysqli_sqlstate" function returns the SQLSTATE error from a previous MySQL operation or in other words this function returns a string containing the SQLSTATE error code for the last error.

Syntax

 
mysqli_sqlstate(connection);

Parameter of the mysqli_sqlstate function

The parameter of the function is:
 
Parameter Description
connection It specifies the MySQL connection to use.

Example

An example of the function is:


<?
php
$
con=mysqli_connect("localhost","root","","mysql");
if(mysqli_connect_errno($con))
{
echo "
Unable to connect: ".mysqli_connect_error();
}
$
qry="CREATE TABLE Dept (deptid INT,deptName VARCHAR(50),empId INT)"
if
(!mysqli_query($con,$qry))
{

echo
"SQL STATE error: ".mysqli_sqlstate($con);
}

mysqli_close
($con);
?>


Output


mysqli-state-function-in-php.jpg

PHP mysqli_ssl_set Function

The PHP MySQLi "mysqli_ssl_set" function establishes a secure connection using SSL and this function always returns the true value and if SSL is incorrectly set then it will return an error when you try to connect.

Syntax

mysqli_ssl_set(connection,key,cert,ca,cipher);

Parameters of the mysqli_sqlstate function

The parameters of the function are:
 
Parameter Description
connection It specifies the MySQL connection to use.
key It specifies the path name to the key file.
cert It specifies the path name to the certificate file.
ca It specifies the path name to the certificate authority file.
cipher It specifies a list of allowable ciphers to use for SSL encryption.

Example

An example of the function is:


<?
php
$
con=mysqli_init();
if
(!$con)
{

die
("Failed.....");
}

mysqli_ssl_set
($con,"key.pem","cert.pem","cacert.pem",NULL,NULL);
if
(!mysqli_real_connect($con,"localhost","root","","mysql"))
{

die
("Connect Error: " . mysqli_connect_error());
}
//
write some PHP code here.
mysqli_close
($con);
?>


PHP mysqli_stat() Function

The PHP MySQLi "mysqli_stat" function returns the current system status and this function returns a string that describes the server status or returns false if an error will occur.

Syntax

mysqli_stat(connection);

Parameter of the mysqli_stat function

The parameter of the function is:
 
Parameter Description
connection It specifies the MySQL connection to use.

Example

An example of the function is:


<?
php
$
con=mysqli_connect("localhost","root","","mysql");
if(mysqli_connect_errno($con))
{
echo "
Unable to connect: ".mysqli_connect_error();
}
echo "
System status: ".mysqli_stat($con);
mysqli_close($con);

?>


Output

mysqli-state-function-in-php.jpg

PHP mysqli_stmt_init() Function

The PHP MySQLi "mysqli_stmt_init" function initializes a statement and returns an object for use with mysqli_stmt_prepare and this  function returns an object.

Syntax

mysqli_stmt_init(connection);

Parameter of the mysqli_stmt_init function

The parameter of the function is:

Parameter Description
connection It specifies the MySQL connection to use.

Example

An example of the function is:

<?php

$con=mysqli_connect("localhost","root","","mysql");

if(mysqli_connect_errno($con))

{

echo "Unable to connect: ".mysqli_connect_error();

}

$Name="Atul";

$stmt=mysqli_stmt_init($con);

if (mysqli_stmt_prepare($stmt,"SELECT id FROM emp WHERE Name=?"))

{

// Bind parameters

mysqli_stmt_bind_param($stmt,"s",$Name);

mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt,$id);

mysqli_stmt_fetch($stmt);

printf("%s <b>id</b> is %s",$Name,$id);

mysqli_stmt_close($stmt);

}

mysqli_close($con);

?>

Output

mysqli-stmt-init-function-in-php.jpg 

PHP mysqli_thread_id() Function

The PHP MySQLi "mysqli_thread_id" function returns the thread id for the current connection.

Syntax

mysqli_thread_id(connection);

Parameter of the mysqli_thread_id function

The parameter of the function is:

Parameter Description
connection It specifies the MySQL connection to use.

Example

An example of the function is:

<?php

$con=mysqli_connect("localhost","root","","mysql");

if(mysqli_connect_errno($con))

{

echo "Unable to connect: ".mysqli_connect_error();

}

$id=mysqli_thread_id($con);

echo $id;

mysqli_kill($con,$id);

mysqli_close($con);

?>

Output


mysqli-thread-id-function-in-php.jpg

PHP mysqli_thread_safe() Function

The PHP MySQLi "mysqli_thread_safe" function returns whether thread safety is given or not and this function returns true if the client library is thread safe otherwise it returns false.

Syntax

mysqli_thread_safe();

Example

An example of the function is:

<?php

$con=mysqli_connect("localhost","root","","mysql");

if(mysqli_connect_errno($con))

{

echo "Unable to connect: ".mysqli_connect_error();

}

echo mysqli_thread_safe();

mysqli_close($con);

?>

Output

mysqli-thread-safe-function-in-php.jpg