JarrodNoonan Posted March 18, 2013 Share Posted March 18, 2013 (edited) Hello, I'm working on a PHP script that allows access through the database. I've added 2 public functions so it'd be easier to access from other pages. My code is: <?php // imports all the necessary files - DO NOT REMOVE. include('gs-config.php'); class gsGlobal { public function establishConnection() { global $configDetails, $securedConnection; $secureConnection = new mysqli($configDetails[0], $configDetails[1], $configDetails[2], $configDetails[3]); if (mysqli_connect_error()) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } } public function addPost() { global $configDetails; $arrayedReturn = array($_POST['title'], $_POST['content'], $_POST['category'], $_POST['keywords']); $obj = new gsGlobal; $obj->establishConnection(); $query = "SELECT * FROM news WHERE id=1"; $result = $secureConnection->query($query); $row = $result->fetch_array(MYSQLI_ASSOC); echo $row['title']; } } ?> However what seems to be the problem is that it doesn't pass the query from the connection, could somebody help me please? Edited March 18, 2013 by JarrodNoonan Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/ Share on other sites More sharing options...
akphidelt2007 Posted March 19, 2013 Share Posted March 19, 2013 (edited) You should be getting an undefined variable for this line... $result = $secureConnection->query($query); as you have not defined $secureConnection in method addPost Edited March 19, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419449 Share on other sites More sharing options...
trq Posted March 19, 2013 Share Posted March 19, 2013 You using classes/functions wrong. Either forget the global keyword exists and pass your data as arguments, or give use global code. Functions, Classes/Methods are designed to encapsulate code. The global keyword is designed to break that encapsulation. Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419459 Share on other sites More sharing options...
Strider64 Posted March 19, 2013 Share Posted March 19, 2013 (edited) I don't know if this will help or confuse you even more, but this is what I do, it works for me. private $db_host = "localhost"; private $db_username = "root"; private $db_password = "******"; private $db_name = "your_database"; public function __construct() { $this->database = new mysqli($this->db_host, $this->db_username, $this->db_password, $this->db_name) or die($this->database->error); // Note to self the die function is not going to be used for production website... } Then I just use $this->database to connect to the database Even though it's a public constructor, I find that I can still encapsulate my other variables and method /functions using protected variables and methods/functions throughout my code. Edited March 19, 2013 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419467 Share on other sites More sharing options...
akphidelt2007 Posted March 19, 2013 Share Posted March 19, 2013 Oh yea, just from personal experience. You will never forgive yourself for not using a static database class. It makes life 1 million times easier. Instead of making it global or instantiating a new db class every single time you want to do a query you simply do DB::runMyQuery("insert query here"). Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419474 Share on other sites More sharing options...
trq Posted March 19, 2013 Share Posted March 19, 2013 Oh yea, just from personal experience. You will never forgive yourself for not using a static database class. It makes life 1 million times easier. Instead of making it global or instantiating a new db class every single time you want to do a query you simply do DB::runMyQuery("insert query here").Static methods are very much frowned upon by most people designing OOP applications. Why? Because they promote tight coupling of components which in turn makes code hard to test. While it may seem to simplify things at first, it just makes larger applications harder to wrok with and more prone to bugs. I would never recommend going down the static path, especially for something like a DBAL. Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419503 Share on other sites More sharing options...
Christian F. Posted March 19, 2013 Share Posted March 19, 2013 Not to mention it makes connecting to more than 1 database at the time impossible. Granted, not a problem for the vast majority of cases, but it is worth taking note of it anyway. This is true for all classes you make "static", after all. Since you can't create objects from it, to hold separate data in. While there can be many objects of the class, there is only one class. Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419533 Share on other sites More sharing options...
akphidelt2007 Posted March 19, 2013 Share Posted March 19, 2013 (edited) Static methods are very much frowned upon by most people designing OOP applications. Why? Because they promote tight coupling of components which in turn makes code hard to test. While it may seem to simplify things at first, it just makes larger applications harder to wrok with and more prone to bugs. I would never recommend going down the static path, especially for something like a DBAL. Everything is frowned upon by someone in the programming world. I would very much suggest using a static db class. Edited March 19, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/275836-php-passing-through-functions/#findComment-1419596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.