I am creating a web application using PHP and MySQL, but I’m facing significant challenges with data integrity. The issue: My database frequently becomes corrupted, resulting in random data loss. Oddly, it is always parts of transactions that are lost, never the full transactions. Details about my setup: I’m utilizing WAMP on a Windows 7 machine to run this locally. It’s strictly for personal use and will not be available online. Below is an excerpt from my MySQL error log:
121109 10:09:56 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
121109 10:09:58 InnoDB: Waiting for the background threads to start
121109 10:09:59 [Note] Crash recovery finished.
The PHP code I’m using includes the following structure:
<?php
$connection = mysqli_connect($host, $username, $password, $database);
mysqli_autocommit($connection, FALSE);
// Add user details
$addUserQuery = "INSERT INTO users (name, email) VALUES (?, ?)";
$stmt1 = mysqli_prepare($connection, $addUserQuery);
mysqli_stmt_bind_param($stmt1, "ss", $userName, $userEmail);
mysqli_stmt_execute($stmt1);
// Add related specifics
$addDetailsQuery = "INSERT INTO user_details (user_id, phone) VALUES (?, ?)";
$stmt2 = mysqli_prepare($connection, $addDetailsQuery);
mysqli_stmt_bind_param($stmt2, "is", $userId, $phoneNumber);
mysqli_stmt_execute($stmt2);
mysqli_commit($connection);
?>
Key points: I am uncertain whether this issue stems from my coding or MySQL settings. As I am relatively new to PHP programming, my code may not be very refined. The query that tends to experience data loss most often is the secondary INSERT statement.