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 compare datetime in MySQL and PHP?
- How to output XML from MySQL using PHP?
- How to use a MySQL union in PHP?
- How to join tables with PHP and MySQL?
- How to list databases in PHP and MySQL?
- How to use a variable in a MySQL query using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to get the version of MySQL using PHP?
- How to generate a UUID in MySQL using PHP?
See more codes...