$sql = "SELECT * FROM users WHERE id = 'nonexistent'"; $result = mysqli_query($conn, $sql); if (!$result) { echo "Error: " . mysqli_error($conn); }
function createUser($conn, $username, $email, $password) { $sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')"; $result = mysqli_query($conn, $sql); if (!$result) { throw new Exception(mysqli_error($conn)); } return true; }In this example, we create a custom function to insert a new user into the database. If the query fails, we use the mysqli_error() function to throw an exception with the error message. Package library: mysqli is a package included in the PHP standard library.