Search the Community
Showing results for tags 'oop syntax'.
-
I am converting my existing code to a more friendly version for PHP5, and since commands like "mysql_real_escape_string" are being deprecated, I figured I would embrace OOP fully moving forward. The problem I am having is the "nesting" of OOP commands. $db_connection = new mysqli("localhost","db_user","db_pw","db_name"); if(mysqli_connect_errno()){ printf("Connection fails: %s\n", mysqli_connect_error()); } $username = $db_connect->real_escape_string($_POST['username']); $stmt = $db_connection->stmt_init(); .... and so on The problem I am having happen when I want to take it one step further and put all that in a class. class Make_Connection { pivate $db_connection function __construct(){ $this->make_connection(); } public function make_connection(){ $this->db_connection = new mysqli("localhost","db_user","db_pw","db_name"); if(!$this->db_connection){ die("Databse connction failed: %s\n", mysqli_connect_error()); } else { $stmt = $this->db_connection->stmt_init() <===========This does not work obviously. ... } } public function clean_sql($value){ $value = $this->db_connection->real_escape_string($value) <===========This does not work obviously. } } $db = new Make_Connection(); Now I know I need "$this" the reference a variable within itself, because this works just fine: public function make_connection(){ $this->db_connection = mysqli_connection("localhost","db_user","db_pw","db_name"); if(!$this->db_connection){ die("Databse connction failed: %s\n", mysqli_error()); } else { $db_select = mysqli_select_db($this->dbconnection, "db_name"); if(!$db_select){ die("Databse selction failed: %s\n", mysqli_error()); } } } Even if I left this part untouch it does not help with the "real_escape_string()". As well, like I said I want to make this most current moving forward, so I would like to impliment the stmt_init() command. I belive it comes down to syntax, if I am wrong please do excuse me. I tried serveral variation, including $this->db_connection::real_escape_strig(), all of them wrong of course. So if someone could clarify where I went wrong, or point me to the correct direction I would be greatful. Thanks all the same in advance.