Home  >  Article  >  Database  >  How to Convert Your Code from MySQL to MySQLi: A Step-by-Step Guide

How to Convert Your Code from MySQL to MySQLi: A Step-by-Step Guide

Linda Hamilton
Linda HamiltonOriginal
2024-10-28 01:50:02349browse

How to Convert Your Code from MySQL to MySQLi: A Step-by-Step Guide

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:

  • MySQLConverterTool: https://github.com/philip/MySQLConverterTool
  • MySQL Shim Library: https://github.com/dshafik/php7-mysql-shim

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn