How can the mysql_query() function in PHP be used to check if a database operation was successful?
To check if a database operation was successful using the mysql_query() function in PHP, you can use the mysql_affected_rows() function to determine the number of affected rows after the query execution. If the number of affected rows is greater than zero, then the operation was successful.
$query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0) {
echo "Database operation was successful.";
} else {
echo "Database operation failed.";
}
Related Questions
- What are the recommended methods for handling user inputs and form submissions in PHP to prevent issues like non-object errors when fetching data from a database?
- How can a PHP script be used to implement a secure file download system?
- How can PHP developers prevent the loss of key-value associations when using sorting functions like rsort on arrays with duplicate values?