MySQLi Function in PHP: Part 9

Introduction

In this article I describe the PHP MySQLi  functions mysqli_kill, mysqli_more_result, mysqli_multi_query, mysqli_next_result and mysqli_num_fields. 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

PHP mysqli_kill() Function

The PHP MySQLi "mysqli_kill" function asks the server to kill a MySQL thread and this function returns true on success or false on failure.

Syntax

 
mysqli_kill(connection,processid)

Parameter in mysqli_kill function

The parameters of the function are:
 
Parameter Description
connection It specifies the MySQL connection to use.
processid It specifies the thread id returned from the mysqli_thread_id)().

Example

An example of the function is:


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

if (mysqli_connect_error($con))

{

echo "Failed  to  connect  to  MySQL:  " . mysqli_connect_error();

}

$id=mysqli_thread_id($con);

mysqli_kill($con,$id);

mysqli_close($con);

?>

PHP mysqli_more_result() Function

The PHP MySQLi "mysqli_more_result" function checks if there are any more query results from a multi-query and this function returns true on success or false on failure.

Syntax

mysqli_more_result(connection)

Parameter in mysqli_more_result 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()) {

    printf("Connect failed: %s\n", mysqli_connect_error());

    exit();

}

 

$query  = "SELECT CURRENT_USER();";

$query .= "SELECT id FROM emp";

/* execute multi query */

if (mysqli_multi_query($con, $query)) {

do

{

if ($result = mysqli_store_result($con)) {

 while ($row = mysqli_fetch_row($result)) {

printf("%s\n", $row[0]);

echo "</br>";

}

mysqli_free_result($result);

}

/* print divider */

if (mysqli_more_results($con)) {

echo "</br>";

printf("-----------------");

echo "</br>";

}

} while (mysqli_next_result($con));

      

}

mysqli_close($con);

?>

Output

mysqli-more-result-function-in-php.jpg

PHP mysqli_multi_query() Function

The PHP MySQLi "mysqli_multi_query" function performs a query on the database and this function returns false if the first statement failed.

Syntax

mysqli_multi_query(connection,query)

Parameter in mysqli_multi_query function

The parameters of the function are:
 
Parameter Description
connection It specifies the MySQL connection to use.
query It specifies one or more queries, separated with columns.

Example

An example of the function is:

<?php

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

if (mysqli_connect_errno()) {

    printf("Connect failed: %s\n", mysqli_connect_error());

    exit();

}

$query  = "SELECT CURRENT_USER();";

$query .= "SELECT id FROM emp";

/* execute multi query */

if (mysqli_multi_query($con, $query)) {

do

{

if ($result = mysqli_store_result($con)) {

 while ($row = mysqli_fetch_row($result)) {

printf("%s\n", $row[0]);

echo "</br>";

}

mysqli_free_result($result);

}

/* print divider */

if (mysqli_more_results($con)) {

echo "</br>";

printf("-----------------");

echo "</br>";

}

} while (mysqli_next_result($con));

      

}

mysqli_close($con);

?>

Output

mysqli-more-result-function-in-php.jpg 

PHP mysqli_next_result() Function

The PHP MySQLi "mysqli_multi_query" function prepares the next result from the multi-query and this function returns true on success or false on failure.

Syntax

mysqli_next_result(connection)

Parameter in mysqli_next_result 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 = new mysqli("localhost", "root", "", "mysql");

if (mysqli_connect_error($con))

{

echo "Failed  to  connect  to  MySQL:  " . mysqli_connect_error();

}

$query  = "SELECT id from emp;";

$query .= "SELECT name FROM bank_coust_info";

/* execute multi query */

if ($con->multi_query($query)) {

do

{

/* store first result set */

if ($result = $con->store_result()) {

while ($row = $result->fetch_row()) {

printf("%s\n", $row[0]);

echo "</br>";

}

$result->close();

}

if ($con->more_results()) {

//echo "</br>";

printf("<b>Second table values</b>");

echo "</br>";

}

} while ($con->next_result());

}

$con->close();

?>

Output

mysqli-next-result-in-php.jpg

PHP mysqli_num_fields() Function

The PHP MySQLi "mysqli_num_filelds" function is used to get the number of fields in a result or say this function returns the number of field from a result set.

Syntax

mysqli_num_filelds(result)

Parameter in mysqli_num_fields function

The parameter of the function is:
 
Parameter Description
result It specifies a result set identifier returned by mysqli_query().

Example

An example of the function is:

<?php

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

if (mysqli_connect_errno($con))

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$sql="SELECT id,name FROM emp ORDER BY name";

if ($result=mysqli_query($con,$sql))

{

$fieldcount=mysqli_num_fields($result);

printf("Result set has %d fields.\n",$fieldcount);

mysqli_free_result($result);

}

mysqli_close($con);

?>

 

Output

mysqli-num-fields-function-in-php.jpg


Similar Articles