CyberShot Posted December 13, 2010 Share Posted December 13, 2010 I have a connection.php for connecting to my database and I have done it this way connection.php function connect() { global $mysql; $mysql = new MySQLi('localhost','cybershot','pascal35','billPay') or die('couldn not connect'. mysql_error()); } I have another file where I want to perform a query so I included the connection.php at the top of that file which I will call results.php and in the body of my results.php I have this $names = $mysql->query("select * from names") or die($mysql->error); if($names){ while($row = $names->fetch_assoc()){ $name = $row['firstName']; $lastName = $row['lastName']; $company = $row['companyName']; echo $name . " "; echo $lastName . " "; echo $company . " "; } } I had to make $mysql a global variable in the connection.php in order to get it to work in another page. is there a better way to pass that variable to the results.php page? or is there a better way period? Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/ Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 any reason u need connect() function? if u remove the function (and of course the global. than just by including the file will connect to the database if your worried about hammering the page, than you can always use defines and an if statement <?php define('InScript',TRUE); include('connect.php'); . . . ?> <?php if(defined('InScript')) $mysql = new MySQLi('localhost','cybershot','pascal35','billPay') or die('couldn not connect'. mysql_error()); ?>[code] Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1146535 Share on other sites More sharing options...
Anti-Moronic Posted December 13, 2010 Share Posted December 13, 2010 You should never have to make variables global within your functions. The functions return. In this case, it would return a new mysqli db object. $mysql = new MySQLi('localhost','cybershot','pascal35','billPay') or die('couldn not connect'. mysql_error()); return $mysql; The problem here however is of improper structure. Your connect function should be within a class that can manage your database connections. The queries you perform should be in classes referred to as 'models'. If you don't have a connection present, you use: $mysql = new MyDbClass(); Then in your construct you can implent the connection() method. To get $names you would have a class called Table_Names or something like that which would utilize the DbClass() to query the database. $nameTable = new Table_Names(); $names = $nameTable->fetchAll(); I understand this is a little more complex than what you have, but it follows a common design patter - MVC (model, view, controller). The above at least, shows an example of how you would implement the model part. This will make your code FAR more manageable and maintainable. A quickfix for this is to learn how to use autoloading. Then you don't have to define anything or include() connection scripts all over. You simply call the db object when you need to. http://php.net/manual/en/language.oop5.autoload.php $mysql = new MyDbClass(); $names = $mysql->query("select * from names") or die($mysql->error); Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1146679 Share on other sites More sharing options...
CyberShot Posted December 13, 2010 Author Share Posted December 13, 2010 I do know what you are talking about here. But creating classes right now is over my head. I will have to study up on what you posted. I understand most of it but don't know the full code to creating everything. I will work on it. Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1146760 Share on other sites More sharing options...
Anti-Moronic Posted December 13, 2010 Share Posted December 13, 2010 If you don't how to create classes, you shouldn't be creating functions. Classes are an easy way to manage groups of functions. Like a validation class which has different functions for validating username, password, email etc Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1146765 Share on other sites More sharing options...
CyberShot Posted December 13, 2010 Author Share Posted December 13, 2010 i see use of functions all the time in php without classes. I created the function to get an idea of how they work. To play with them if you will. I would much rather create the classes and do it that way. As for right now, it is beyond me. I will work on it in the next few days. First, I want to get the code working as is and then rewrite it to make it better....More correct. If I have a working prototype to go off of, it will help me troubleshoot since I know it should work. Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1146766 Share on other sites More sharing options...
laffin Posted December 14, 2010 Share Posted December 14, 2010 Bahh, OOP programming is just a strict methodology of programming. U can use functions without classes, as C became before C++ not after Procedural coding is just as viable methodology of progamming as OOP is. So its not a case of don't use functions without OOP. its a case of Know when to use a function. the connect is a usually a 1 time call, so there is no reason to put this into a function but say u are coding along, and u notice that u continually repeat code. this is where u should use a function. Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1147044 Share on other sites More sharing options...
CyberShot Posted December 14, 2010 Author Share Posted December 14, 2010 Okay, I tried setting up a class structure instead of using functions. I created MyClasses.php and so far just have this in it class mysql { function connect(){ $mysql = new MySQLi('localhost','cybershot','pascal35','billpay') or die($mysql->error); } } now in my index page, I included the MyClasses.php file and down where I wanted to perform a query, I have this $mysql = new mysql(); $mysql->connect(); $result = $mysql->query("SELECT * FROM names") or die($mysql->error); but now I get an error saying Call to undefined method mysql::query() in C:\wamp\www\BillPay\index.php on line 52 which should be this line $result = $mysql->query("SELECT * FROM names") or die($mysql->error); Quote Link to comment https://forums.phpfreaks.com/topic/221486-the-propper-way-to-organize-my-code/#findComment-1147435 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.