I have been trying to make this work for a couple of days now. I am wanting to evolve my knowledge of PHP and database security and am looking to use OOPHP and paramterized queries. I figured I would start off by simply connecting to the database and performing a select and displaying the results. I know the paramaterized query works on its own, but when I put it inside a class and call it, it won't grab the results and display them. Here is my code I've been working on:
<?php
class connect
{
public static function run()
{
$mysqli = new mysqli("*****", "*****", "*****");
$db = "sales";
mysqli_select_db($db);
if(!$mysqli)
{
printf("Connection Failed: %s
", mysqli_connect_error());
}
else
{
echo("MariaDB Connection Successfull!!!");
}
}
}
//call_user_func(array($className, 'run'));
class contacts
{
public function select()
{
if($stmt = $mysqli->prepare("SELECT CON_ID, CON_FNAME, CON_LNAME, CON_EMAIL FROM contacts"))
{
$stmt->execute();
$stmt->bind_result($id,$fname,$lname,$email);
echo("<table align='center' width='40%'");
echo("<tr>");
echo("<td align='center' width='25%'>Contact ID</td>");
echo("<td align='center' width='25%'>First Name</td>");
echo("<td align='center' width='25%'>Last Name</td>");
echo("<td align='center' width='25%'>Email Address</td>");
echo("</tr>");
while($stmt->fetch())
{
echo("<tr>");
echo("<td align='center' width='25%'>");
printf("%s",$this->$id);
echo("</td>");
echo("<td align='center' width='25%'>");
printf("%s",$this->$fname);
echo("</td>");
echo("<td align='center' width='25%'>");
printf("%s",$this->$lname);
echo("</td>");
echo("<td align='center' width='25%'>");
printf("%s",$this->$email);
echo("</td>");
echo("</tr>");
}
printf("</table>");
$stmt->close();
}
else
{
printf("Prepared Statement Error: %s
", $mysqli->error());
}
}
}
$connect = "connect";
$connect::run();
$contact = "contacts";
$contact::select();
?>
When I run the page, the only output I receive is "MariaDB Connection Successful!!!", but it doesn't seem to run the select() within the class "contacts". I am looking to learn better ways of coding with PHP, so if there is a better way, I would love to learn it!
I would be very grateful for any help that anyone can give.