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.$connis the connection to the database.mysqli_affected_rows($conn): This is the function used to check the number of rows affected by the query.ifstatement: This is used to check if the query was successful or not.
Helpful links
More of Php Mysql
- How to update to null value in MySQL using PHP?
- How to create an SSL connection to MySQL using PHP?
- How to check if a record exists in PHP and MySQL?
- How to change database in MySQL with PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to generate a UUID in MySQL using PHP?
- How to set a timeout for MySQL query in PHP?
- How to insert a null value in MySQL using PHP?
- How to get table column names in PHP MySQL?
- How to export data from MySQL to Excel using PHP?
See more codes...