What are the potential risks associated with using mysql_fetch_object() in PHP scripts?

Using mysql_fetch_object() in PHP scripts can lead to security vulnerabilities such as SQL injection if not properly sanitized. To mitigate this risk, it is recommended to use prepared statements with parameterized queries when interacting with a MySQL database in PHP.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare a statement
$stmt = $mysqli->prepare("SELECT * FROM table WHERE id = ?");

// Bind parameters
$stmt->bind_param("i", $id);

// Execute the statement
$stmt->execute();

// Bind result variables
$stmt->bind_result($col1, $col2, $col3);

// Fetch results
while ($stmt->fetch()) {
    // Process the fetched data
}

// Close the statement and connection
$stmt->close();
$mysqli->close();