php-mysqlHow to check the result of an insert in PHP and MySQL?
To check the result of an insert in PHP and MySQL, you can use the mysqli_affected_rows()
function. This function returns the number of rows affected by the last INSERT
, UPDATE
, REPLACE
or DELETE
query.
Example code
$sql = "INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3)";
$result = mysqli_query($conn, $sql);
if (mysqli_affected_rows($conn) > 0) {
echo "Insert successful";
} else {
echo "Insert failed";
}
Output example
Insert successful
Code explanation
$sql
: This is the SQL query used to insert data into the table.$result
: This is the result of the query.mysqli_query($conn, $sql)
: This is the function used to execute the query.$conn
is the connection to the database.mysqli_affected_rows($conn)
: This is the function used to check the number of rows affected by the query.if
statement: This is used to check if the query was successful or not.
Helpful links
More of Php Mysql
- How to join tables with PHP and MySQL?
- How to use a variable in a MySQL query using PHP?
- How to update to null value in MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to fetch data from MySQL in PHP?
- How to check if a record exists in PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to escape a string for MySQL in PHP?
- How to convert MySQL datetime to string in PHP?
- How to export data from MySQL to Excel using PHP?
See more codes...