If you just want to grab one record, or know that you are only expecting one result you can use the following code:
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name", "Port Number If Needed");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Limit to one record
$stmt = mysqli_prepare($conn, "SELECT Field1, Field2, etc FROM YourTable WHERE ID = ? LIMIT 1");
// Bind and Execute our statement
mysqli_stmt_bind_param($stmt, "i", $ID);
mysqli_stmt_execute($stmt);
// Get result set
$result = mysqli_stmt_get_result($stmt);
// Get the single row from the result set ($result) (No loop needed!)
$SingleRow = mysqli_fetch_assoc($result);
// 5. Use the data
if ($SingleRow) {
// We have a single row returned
echo "<h1>Editing: " . htmlspecialchars($SingleRow['Field1']) . "</h1>";
} else {
echo "Record not found.";
}
mysqli_stmt_close($stmt);
mysqli_close($conn)
?>
Disclaimer: The code on this website is provided "as is" and comes with no warranty. The author of this website does not accept any responsibility for issues arising from the use of code on this website. Before making any significant changes, ensure you take a backup of all files and do not work directly on a live/production website without thoughly testing your changes first.