I am trying to convert over to MySQLi and wanted expert advice. Is the enclosed code 100% OOP. Also, how secure is the code against attacks? I know nothing is 100% secure, but how good/safe is it? How and what can I make better? This is just a generic query as I wanted to get a basic model setup.
<?php
define ('DB_HOST', 'localhost'); // Host name
define ('DB_USER', 'root'); // Mysql username
define ('DB_PASSWORD', 'password'); // Mysql password
define ('DB_NAME', 'database'); // Database name
// New Connection
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
require("dbcon.php"); // This is actually the code above in a separate file that I have included here for checking.
$query = "SELECT id, username, firstname FROM users WHERE firstname=?";
if ($stmt = $mysqli->prepare($query))
{
// Bind the variables
$firstname = $_POST[first_name];
// Bind parameters s - String, b - Boolean, i - Integer etc
$stmt->bind_param("s", $firstname);
// Execute it
$stmt->execute();
// Store it for num rows
$stmt->store_result();
$num_rows = $stmt->num_rows;
// Assign blank variables
$id = "";
$username = "";
$firstname = "";
// Bind the result and retrieve the data
$stmt->bind_result($id, $username, $firstname);
if(($num_rows) > 0)
{
while($stmt->fetch())
{
echo $id;
echo "<br />";
echo $username;
echo "<br />";
echo $password;
echo "<br />";
echo "Number of rows: ", $num_rows;
echo "<br />";
echo "<br />";
}
}
else
{
echo "No records";
}
// Close the statement
$stmt->close();
}
// Close connection
$mysqli->close();
?>