PHP mysqli_stmt_param_count() Function
Definition and Usage
The mysqli_stmt_param_count() function accepts a (prepared) statement object as a parameter and returns the number of parameter markers in it.
Syntax
mysqli_stmt_param_count($stmt)
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
stmt(Mandatory) This is an object representing a statement executing a SQL query. |
Return Values
PHP mysqli_stmt_param_count() function returns an integer value indicating the number of parameter markers in the given prepared statement.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Assume we have created a table named employee in the MySQL database with the following contents $minus;
mysql> select * from employee; +------------+--------------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+--------------+------+------+--------+ | Vinay | Bhattacharya | 20 | M | 21000 | | Sharukh | Sheik | 25 | M | 23300 | | Trupthi | Mishra | 24 | F | 51000 | | Sheldon | Cooper | 25 | M | 2256 | | Sarmista | Sharma | 28 | F | 15000 | +------------+--------------+------+------+--------+ 5 rows in set (0.00 sec)
Following example demonstrates the usage of the mysqli_stmt_param_count() function (in procedural style) −
<?php
$con = mysqli_connect("localhost", "root", "password", "mydb");
$stmt = mysqli_prepare($con, "UPDATE employee set INCOME=INCOME-? where INCOME>=?");
mysqli_stmt_bind_param($stmt, "si", $reduct, $limit);
$limit = 20000;
$reduct = 5000;
//Executing the statement
mysqli_stmt_execute($stmt);
print("Records Updated......\n");
//Affected rows
$count = mysqli_stmt_param_count($stmt);
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
print("Rows affected ".$count);
?>
This will produce following result −
Records Updated...... Rows affected 3
Example
In object oriented style the syntax of this function is $stmt->param_count; Following is the example of this function in object oriented style $minus;
<?php
//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");
$con -> query("CREATE TABLE myplayers(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
print("Table Created.....\n");
$stmt = $con -> prepare( "INSERT INTO myplayers values(?, ?, ?, ?, ?)");
$stmt -> bind_param("issss", $id, $fname, $lname, $pob, $country);
$id = 1;
$fname = 'Shikhar';
$lname = 'Dhawan';
$pob = 'Delhi';
$country = 'India';
//Executing the statement
$stmt->execute();
//Affected rows
$count = $stmt ->param_count;
print("Number of parameters: ".$count);
//Closing the statement
$stmt->close();
//Closing the connection
$con->close();
?>
This will produce following result −
Table Created..... Records Deleted..... Number of parameters: 5