tsilenzio Posted July 3, 2007 Share Posted July 3, 2007 Is having a function to do querys good or bad? Like the following <?php // functions.php - All needed functions function processQuery($sql) { require_once("config.php"); if(!$result = mysql_query($sql, $conn)) { die(mysql_error()); } } ?> Incase you need it - config.php <?php // config.php - Database configurations // Hostname exp: localhost, www.somesite.com $db_hostname = "some_domain_name"; // User for database $db_username = "some_user"; // Password for database $db_password = "some_password"; // Database $db_database = "some_databadr"; $conn = mysql_connect($db_hostname, $db_username, $db_password); if (!$conn) { die("Couldn't connect to database"); } else { mysql_select_db($db_database, $conn); } ?> Main questions i have is will that work if i use like require_once(); functions [for other pages like login.php] , and will that ork or have you spoted an error? =/ Thanx in advance! Quote Link to comment Share on other sites More sharing options...
sandrob57 Posted July 3, 2007 Share Posted July 3, 2007 Not bad, I always use functions. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 3, 2007 Share Posted July 3, 2007 Its a good idea not bad. Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 3, 2007 Author Share Posted July 3, 2007 sweet , by the way if you can make it better go ahead Quote Link to comment Share on other sites More sharing options...
corbin Posted July 3, 2007 Share Posted July 3, 2007 Unless you're going to extend that function, I see no use for it.... You can just set your error reporting higher while you're testing for stuff like that since theoretically opnce your script is in production a query will never fail anyway. Also, I can sorta see why a function could be useful, and I must suggest returning the resource if you do use a function so that you can preform functions on the resource ID to find the number of rows and so on.... Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 3, 2007 Author Share Posted July 3, 2007 ok ill work on that lol (this is honestly my first time writing PHP code) Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 3, 2007 Share Posted July 3, 2007 on creating the sql query function its better to create separate function for separate uses say for delete add and select hope that helps :-* :-* Quote Link to comment Share on other sites More sharing options...
corbin Posted July 3, 2007 Share Posted July 3, 2007 Oh... ;p Also, the way your function is right now, it could easily be replicated without the function with mysql_query($query) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 3, 2007 Author Share Posted July 3, 2007 yea i was thinking of a way to do that etheir diff function or possibly somrthing better well i got work tommrow and as always i do nothing so i got 8 hours to think of it, im hitting the sack its late here, thanx again guys! Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 3, 2007 Author Share Posted July 3, 2007 yea lol im lazy if it saves me like 2 letters ill do it xD. Also i was kidna hoping i could come up with a way to make it eaiser to do it like for create, select, delete, drop.. and so on.. Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 11, 2007 Author Share Posted July 11, 2007 Well heres my new update jsut incase ne1 wanted to see it lol function processQuery($query) { global $root_path; require_once($root_path . 'extension.inc'); require_once($root_path . 'config.' . $phpEx); if(in_array('mysqli', get_loaded_extensions())) { $conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_database); if(!mysqli_connect_error()) { if($result = mysqli_query($conn, $query)) { return $result; } else { die(mysqli_error($conn)); } } else { die(mysqli_connect_error()); } } elseif(in_array('mysql', get_loaded_extensions())) { if($conn = mysql_connect($db_hostname, $db_username, $db_password)) { if(mysql_select_db($db_database, $conn)) { if($result = mysql_query($sql, $conn)) { return $result; } else { die(mysql_error()); } } else { die(mysql_error()); } } else { die(mysql_error()); } } else { die('MySQL extensions are not loaded'); } } Just got done writing it not sure if it works 100% but im going to try it hopefully i wont get alot of errors lol not really use to doing stuff with my_sqli functions use to normal my_sql functions. I just got introduced to my_sqli functions NOTE: All thats inside include.inc is a var which is: $phpEx = 'php'; which u can change if u decide to make ur pages end in a diffrent ending for security reasons And $root_path would be decleared on the page that calls the processQuery function, the variable would be the root (like if you 3 directorys away (into not out) from the starting directory it would be: $root_path = './../../../'; Quote Link to comment Share on other sites More sharing options...
steelmanronald06 Posted July 11, 2007 Share Posted July 11, 2007 Also, you might want to create other functions for the different mysql queries. Such as select, delete, update, etc. But, put all those into a class and do it object oriented: <?php class MySQL { function select(){ // select query } function delete() { // delete query } } ?> and so on and so forth. you could even make a class that gathers information, such as your database connection information, and then have the msyql class extend the config class. The possibilities of OOP are endless! Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 11, 2007 Author Share Posted July 11, 2007 I know lol but to be honest havnt made any classes yet so when i get to doing that trust me il make cool stuff for right now i like this because i use 2 diffrent sites one has MySQL 4.x and one has MySQL 5.x well the one using 4.x wont accept mysqli function and 5.0 wont accept mysql functions. also it auto loads the config lol so it less typing for me Yea later i plan to add like SELECT and DELETE and all that along with WHERE and such so all u have to send is vars unless that makes it more complicated to be honest i havnt done tooo much stuff with mysql yet Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 I'm a nooblet at Classes, does PHPFreak have a tutorial? (Only ever used Classes in Python myself) Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 11, 2007 Author Share Posted July 11, 2007 to be honest I dont know ive only used classes in Java, and even then it was confusign when we got int oabstract classes and really deep down the rabit whole =/ Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 I like using a mixture of both classes and functions. I have a database class created. Which has my own special functions like query etc. Then I have a db functions page, which I create functions that access the class functions, this way I do not have to pass the DB Class to other classes I just have to call the functions. But I get my error handling and logging features that the class has to offer, such as "how many queries ran" etc. Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 12, 2007 Author Share Posted July 12, 2007 yea Im going to make one of those when i learn more about classes but as of right now classes make me confused lol how do you keep a class alive like I know u create a clas but once u change the page won't it die? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 yea Im going to make one of those when i learn more about classes but as of right now classes make me confused lol how do you keep a class alive like I know u create a clas but once u change the page won't it die? Yea, it will die unless you use sessions to store the class in =) Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted July 12, 2007 Share Posted July 12, 2007 I use a DB class also. It's very handy and you can add a lot of stuff in for testing, it also cleans up your normal code a lot too. Quote Link to comment 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.