Object Oriented Style

  $stmt = $maindb->prepare("UPDATE some_table SET
        MyStringColumn = ?,
        MyIntColumn = ?
      WHERE MyColumn = ?
  $stmt->bind_param("sii", $MyStringColumn, $MyIntColumn, $MyRowId);
How many rows were affected
  $NumOfAffectedRows = $stmt->affected_rows;

Procedural Style

	$result = mysqli_query($dblink, "UPDATE some_table SET
						image_filename = '',
						something_else = 4
					WHERE indexer = $destid
	if ( ($result != TRUE) || (mysqli_affected_rows($dblink) < 1) )
		die("ERROR - Couldn't update");
Maths Operations In Update Queries
	mysqli_query($dblink, "UPDATE some_table SET some_value_a = some_value_a + 1, some_value_b = some_value_b + 10  WHERE id = 1");
Add text to an existing text field
  UPDATE MyTable SET MyFieldName = CONCAT_WS('', MyFieldName, ' [UserBlocked ]') WHERE user_id = %d"

  UPDATE MyTable SET MyFieldName = CONCAT_WS('', MyFieldName, ' [UserBlocked ', Now(), ' Reason: %d]') WHERE user_id = %d"

  //N.B. CONCAT_WS allows this to work if the field is currently null (CONCAT will fail) and the first '', is required
  //as it is what CONCAT_WS inserts between each of the following string fields (nothing in this case)
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.