Three Different Approaches to MySql Queries
mysqli (objects) |
query("SELECT * FROM " . $tname . " WHERE fname LIKE '$fname'");
if ($res == 0) {
echo("Error " . mysqli_errno() . ": " . mysqli_error() . "");
} else if ($con->field_count == 0) {
echo("Query executed successfully");
} else {
/* Get field information for all columns */
$finfo = $res->fetch_fields();
foreach ($finfo as $val) {
echo("" . $val->name . " | ");
}
echo("
");
$res = $con->real_query("SELECT * FROM persons WHERE fname LIKE '$fname'");
if ($result = $con->store_result()) {
while ($row = $result->fetch_row()) {
echo(" | ");
for ($jj = 0; $jj < $con->field_count; $jj++) {
echo("" . $row[$jj] . " | ");
}
echo("
");
}
$result->close();
}
echo("
");
}
$con->close($con);
?>
mysql (functional) |
Error " . mysql_errno() . ": " . mysql_error() . "");
} else if (mysql_num_rows($res) == 0) {
echo("Query executed successfully");
} else {
for ($ii = 0; $ii < mysql_num_fields($res); $ii++) {
echo("" . mysql_field_name($res,$ii) . " | ");
}
echo("
");
while (($r_array = mysql_fetch_row($res))) {
echo(" | ");
for ($jj = 0; $jj < mysql_num_fields($res); $jj++) {
echo("" . $r_array[$jj] . " | ");
}
echo("
");
}
echo("
");
}
mysql_close($link);
?>
PDO (objects) |
table;
$stmt = $dbh->prepare($sql);
try {
if($stmt->execute()){
$raw_column_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($raw_column_data as $outer_key => $array){
foreach($array as $inner_key => $value){
if (!(int)$inner_key){
//$this->column_names[] = $value;
echo("" . $value . " | ");
}
}
}
}
} catch (Exception $e){
return $e->getMessage(); //return exception
}
echo("
");
$STH = $dbh -> prepare( "SELECT * FROM " . $tname . " WHERE fname LIKE '$fname'" );
if ($STH -> execute()) {
$raw_column_data = $STH->fetchAll(PDO::FETCH_ASSOC);
foreach($raw_column_data as $outer_key => $array){
echo(" | ");
foreach($array as $inner_key => $value){
if (!(int)$inner_key){
echo("" . $value . " | ");
}
}
echo("
");
}
}
$dbh->close();
} catch(PDOException $e) {
echo $e->getMessage();
}
echo("
");
?>