I have a generic script that I use when running queries on a database, which encompasses error checking and the like. Hopefully, this is a readable enough script.
// Set some variables if necessary
$var = "example";
// Write sql statement with ? as placeholders for any values
$sql = "sqlstatementhere";
// Prepare the SQL statement using the database connection parameter
if($stmt = $dbconEDB->prepare($sql))
{
// Bind any necessary variables
if($stmt->bind_param('s', $var))
{
$result = $stmt->execute();
// If the statement ran successfully
if($result)
{
$result = $stmt->get_result();
if($result->num_rows >= 1)
{
while($row = $result->fetch_assoc())
{
// If there are result get them here
// $var = $row['fieldname'];
}
}
else // the statement returned 0 results
{
// Deal with the nothingness
}
}
else // the sql didnt execute
{
// Somethings gone wrong here
}
}
else // the binding was wrong
{
// Check your bindings
}
}
else // There was an error preparing the sql statement (its wrong)
{
// the sql is wrong
}
I want to make the same kind of thing with PDO, however, would it be more beneficial to just make a Class?