PHP: Hypertext Preprocessor (original) (raw)
mysqli_stmt::execute
mysqli_stmt_execute
(PHP 5, PHP 7, PHP 8)
mysqli_stmt::execute -- mysqli_stmt_execute — Executes a prepared statement
Description
Object-oriented style
If the statement is UPDATE
, DELETE
, or INSERT
, the total number of affected rows can be determined by using the mysqli_stmt_affected_rows() function. If the query yields a result set, it can be fetched usingmysqli_stmt_get_result() function or by fetching it row by row directly from the statement usingmysqli_stmt_fetch() function.
Parameters
statement
Procedural style only: A mysqli_stmt object returned by mysqli_stmt_init().
params
An optional list array with as many elements as there are bound parameters in the SQL statement being executed. Each value is treated as a string.
Return Values
Returns [true](reserved.constants.php#constant.true)
on success or [false](reserved.constants.php#constant.false)
on failure.
Changelog
Version | Description |
---|---|
8.1.0 | The optional params parameter has been added. |
Examples
Example #1 Execute a prepared statement with bound variables
Object-oriented style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); mysqli=newmysqli("localhost","myuser","mypassword","world");mysqli = new mysqli("localhost", "my_user", "my_password", "world");mysqli=newmysqli("localhost","myuser","mypassword","world");mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");/* Prepare an insert statement / stmt=stmt = stmt=mysqli->prepare("INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");/ Bind variables to parameters / stmt−>bindparam("sss",stmt->bind_param("sss", stmt−>bindparam("sss",val1, val2,val2, val2,val3);$val1 = 'Stuttgart';
$val2 = 'DEU';
$val3 = 'Baden-Wuerttemberg';/ Execute the statement / stmt−>execute();stmt->execute();stmt−>execute();val1 = 'Bordeaux';
$val2 = 'FRA';
$val3 = 'Aquitaine';/ Execute the statement /
$stmt->execute();/ Retrieve all rows from myCity */ query="SELECTName,CountryCode,DistrictFROMmyCity";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">result=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>mysqli−>query(query = "SELECT Name, CountryCode, District FROM myCity"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">result = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>mysqli->query(query="SELECTName,CountryCode,DistrictFROMmyCity";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">result=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>mysqli−>query(query);
while ($row = $result->fetch_row()) {
printf("%s (%s,%s)\n", row[0],row[0], row[0],row[1], $row[2]);
}`
Procedural style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); link=mysqliconnect("localhost","myuser","mypassword","world");mysqliquery(link = mysqli_connect("localhost", "my_user", "my_password", "world");mysqli_query(link=mysqliconnect("localhost","myuser","mypassword","world");mysqliquery(link, "CREATE TEMPORARY TABLE myCity LIKE City");/* Prepare an insert statement / stmt=mysqliprepare(stmt = mysqli_prepare(stmt=mysqliprepare(link, "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");/ Bind variables to parameters /
mysqli_stmt_bind_param($stmt, "sss", val1,val1, val1,val2, val3);val3);val3);val1 = 'Stuttgart';
$val2 = 'DEU';
$val3 = 'Baden-Wuerttemberg';/ Execute the statement /
mysqli_stmt_execute($stmt);$val1 = 'Bordeaux';
$val2 = 'FRA';
$val3 = 'Aquitaine';/ Execute the statement /
mysqli_stmt_execute($stmt);/ Retrieve all rows from myCity */ query="SELECTName,CountryCode,DistrictFROMmyCity";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">result=mysqliquery(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1.0361em;vertical−align:−0.2861em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">ys</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">ql</span><spanclass="mord"><spanclass="mordmathnormal">i</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight"style="margin−right:0.03588em;">q</span></span></span></span><spanclass="vlist−s"></span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.2861em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">ery</span><spanclass="mopen">(</span></span></span></span>link,query = "SELECT Name, CountryCode, District FROM myCity"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">result = mysqli_query(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">ys</span><span class="mord mathnormal" style="margin-right:0.01968em;">ql</span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">q</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.03588em;">ery</span><span class="mopen">(</span></span></span></span>link, query="SELECTName,CountryCode,DistrictFROMmyCity";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">result=mysqliquery(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1.0361em;vertical−align:−0.2861em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">ys</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">ql</span><spanclass="mord"><spanclass="mordmathnormal">i</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight"style="margin−right:0.03588em;">q</span></span></span></span><spanclass="vlist−s"></span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.2861em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">ery</span><spanclass="mopen">(</span></span></span></span>link,query);
while ($row = mysqli_fetch_row($result)) {
printf("%s (%s,%s)\n", row[0],row[0], row[0],row[1], $row[2]);
}`
The above examples will output:
Stuttgart (DEU,Baden-Wuerttemberg) Bordeaux (FRA,Aquitaine)
Example #2 Execute a prepared statement with an array of values
Object-oriented style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); mysqli=newmysqli(′localhost′,′myuser′,′mypassword′,′world′);mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');mysqli=newmysqli(′localhost′,′myuser′,′mypassword′,′world′);mysqli->query('CREATE TEMPORARY TABLE myCity LIKE City');/* Prepare an insert statement / stmt=stmt = stmt=mysqli->prepare('INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)');/ Execute the statement /
$stmt->execute(['Stuttgart', 'DEU', 'Baden-Wuerttemberg']);/ Retrieve all rows from myCity */ query=′SELECTName,CountryCode,DistrictFROMmyCity′;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">result=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>mysqli−>query(query = 'SELECT Name, CountryCode, District FROM myCity'; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">result = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>mysqli->query(query=′SELECTName,CountryCode,DistrictFROMmyCity′;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">result=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>mysqli−>query(query);
while ($row = $result->fetch_row()) {
printf("%s (%s,%s)\n", row[0],row[0], row[0],row[1], $row[2]);
}`
Procedural style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); link=mysqliconnect(′localhost′,′myuser′,′mypassword′,′world′);mysqliquery(link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');mysqli_query(link=mysqliconnect(′localhost′,′myuser′,′mypassword′,′world′);mysqliquery(link, 'CREATE TEMPORARY TABLE myCity LIKE City');/* Prepare an insert statement / stmt=mysqliprepare(stmt = mysqli_prepare(stmt=mysqliprepare(link, 'INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)');/ Execute the statement /
mysqli_stmt_execute($stmt, ['Stuttgart', 'DEU', 'Baden-Wuerttemberg']);/ Retrieve all rows from myCity */ query=′SELECTName,CountryCode,DistrictFROMmyCity′;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">result=mysqliquery(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1.0361em;vertical−align:−0.2861em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">ys</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">ql</span><spanclass="mord"><spanclass="mordmathnormal">i</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight"style="margin−right:0.03588em;">q</span></span></span></span><spanclass="vlist−s"></span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.2861em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">ery</span><spanclass="mopen">(</span></span></span></span>link,query = 'SELECT Name, CountryCode, District FROM myCity'; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">result = mysqli_query(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">ys</span><span class="mord mathnormal" style="margin-right:0.01968em;">ql</span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">q</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.03588em;">ery</span><span class="mopen">(</span></span></span></span>link, query=′SELECTName,CountryCode,DistrictFROMmyCity′;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">result=mysqliquery(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">res</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">lt</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1.0361em;vertical−align:−0.2861em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">ys</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">ql</span><spanclass="mord"><spanclass="mordmathnormal">i</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight"style="margin−right:0.03588em;">q</span></span></span></span><spanclass="vlist−s"></span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.2861em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">ery</span><spanclass="mopen">(</span></span></span></span>link,query);
while ($row = mysqli_fetch_row($result)) {
printf("%s (%s,%s)\n", row[0],row[0], row[0],row[1], $row[2]);
}`
The above examples will output:
Stuttgart (DEU,Baden-Wuerttemberg)
See Also
- mysqli_execute_query() - Prepares, binds parameters, and executes SQL statement
- mysqli_prepare() - Prepares an SQL statement for execution
- mysqli_stmt_bind_param() - Binds variables to a prepared statement as parameters
- mysqli_stmt_get_result() - Gets a result set from a prepared statement as a mysqli_result object
Found A Problem?
18 years ago
`Just to clarify this note in the Manual regarding this function:
"Note: When using mysqli_stmt_execute(), the mysqli_stmt_fetch() function must be used to fetch the data prior to performing any additional queries."
This is because this function DOES NOT store the result set on the client side so you have to fetch everything in the result set or else you risk major errors.
If you however use the function mysqli_stmt_store_result immediately after you use this function, you are forcing the result set to be stored on the client side and thus it is safe to issue extra queries before fetching all the data.
This is where you really have to make a choice regarding on your application's priorities. If you know your result set is memory hefty, then its a good idea not to store it on the client side so you don't run in any errors regarding unavailable memory on the server. But this also means your not going to do a lot of calculations on the result set or else you will prevent any other usage of the table to which the result set came from until you fetched it all.
If your going to do a lot of calculations or your result set is not memory hefty, its probably a good idea to store it on the client side.
Most of these problems can easily be solved if you have a lot of memory available on your server but thats usually not the case for those on shared hosting.
An intelligent way to counter this problem if your on a shared host is to be smart in the way you design your queries. Try to limit the result set if you know you will be fetching memory hefty result sets.
Test different alternatives for your application and see what works best for you under different conditions.
Good Luck,
`