Converting MySQL to MySQLi
Since MySQL is now deprecated, upgrading to MySQLi is essential. However, if you're not familiar with MySQLi_*, it can be daunting, especially with a site coded in MySQL.
To simplify the transition, here's how to convert the provided MySQL code to MySQLi:
$connection = new mysqli("host", "username", "password", "database_name"); if ($connection->connect_errno) { printf("Error: %s\n", $connection->connect_error); } $sql_follows = "SELECT * FROM friends WHERE user1_id=? AND status=? OR user2_id=? AND status=?"; $stmt = $connection->prepare($sql_follows); $stmt->bind_param("iiii", $_SESSION['id'], 2, $_SESSION['id'], 2); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { // Do something with the data } $result->close(); $stmt->close(); $connection->close();
Automated Conversion Tools
To automate the conversion process, you can utilize:
Remember, the generated code may not be optimal. However, it can provide a starting point and help you better understand MySQLi.
The above is the detailed content of How to Convert Your Code from MySQL to MySQLi: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!