Skip to content
PHP Logo

What are the two different syntaxes for a MySQL update command in PHP?

When updating data in MySQL using PHP, there are two different syntaxes that can be used for the update command. The first syntax involves using a string query with placeholders for the values to be updated, while the second syntax involves using a prepared statement with bind parameters. Using prepared statements is generally considered more secure as it helps prevent SQL injection attacks.

php
// First syntax using string query
$sql = "UPDATE table_name SET column1 = 'new_value' WHERE condition = 'value'";
$result = $conn->query($sql);

// Second syntax using prepared statement with bind parameters
$stmt = $conn->prepare("UPDATE table_name SET column1 = ? WHERE condition = ?");
$stmt->bind_param("ss", $new_value, $condition_value);

$new_value = "new_value";
$condition_value = "value";
$stmt->execute();
$stmt->close();