I'm used to OOP, but haven't used it much with PHP so the syntax and declarations are a bit hazy. Used it more often with C++ and Java, and whatever bastardized version of C++ arduino runs off of. I have used mysqli in the past so I do know that there are a few things that are different, how it takes arguments, etc. I am trying to get a basic grasp back on OOP syntax and database querying with php after being gone from it for years working in an industrial occupation.
Currently rewriting my code with more updated syntax and function libraries. Having issues declaring protected or public variables as they seem to go undefined when it comes time to call them. Now bear with me, declaring your own constructors wasn't really necessary back when I was at it, so I'm still trying to wrap my head around the concept of it. I seem to be failing pretty hard. This code throws me an "UNEXPECTED T_VARIABLE" on line 6.
<?php
class Sql {
protected $host, $user, $pass, $db;
$host = "localhost";
$user = "admin";
$pass = "";
$db = "blog";
public function __construct($host, $user, $pass, $db){
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->db = $db;
$conn = mysqli_connect($host, $user, $pass);
mysqli_select_db($db);
}
public function userData() {
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
for($i = 0; $i < $num; $i++){
$data = mysqli_fetch_array($result);
echo $data['name'];
echo " | ";
echo $data['email'];
echo " | ";
echo $data ['phone'];
echo "<br />";
}
}
}
?>