RCS Posted July 25, 2008 Share Posted July 25, 2008 I wrote my database connections.... as function, now I need someone who can help to explain how I would use them cause I keep gettin errors.... Thanks in advance!!! here is my database functions <?php require_once 'config.inc'; $con = mysql_connect($db_host, $db_user,$db_pass) or die ('Could not make a connection to database.'); mysql_select_db($db_name) or die ('Could not select the database.'); function dbQuery($sql) { return mysql_query($sql) or die('Query failed. ' . mysql_error()); } function dbAffectedRows() { global $Con; return mysql_affected_rows($Con); } function dbFetchArray($result, $resultType = MYSQL_NUM) { return mysql_fetch_array($result, $resultType); } function dbFetchAssoc($result) { return mysql_fetch_assoc($result); } function dbFetchRow($result) { return mysql_fetch_row($result); } function dbFreeResult($result) { return mysql_free_result($result); } function dbNumRows($result) { return mysql_num_rows($result); } function dbSelect($db_name) { return mysql_select_db($db_name); } ?> Link to comment https://forums.phpfreaks.com/topic/116517-functions/ Share on other sites More sharing options...
RCS Posted July 25, 2008 Author Share Posted July 25, 2008 e.g. I have two queries to the database how would I rewrite these to use with my functions.... <?php $uinfo = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'") or die(mysql_error()); $checkuser = mysql_num_rows($uinfo); if($checkuser == '0') { echo "Username not found"; }else{ //fetch the sql $udata = mysql_fetch_array($uinfo); if($udata[userlevel] == 1) { echo "This account had not been verified."; } else //if the db password and the logged in password are the same login if($udata[password] == $password) { $query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'") or die(mysql_error()); //fetchs the sql $user = mysql_fetch_array($query); //sets the logged session $_SESSION['id'] = "$user[id]"; $_SESSION['password'] = "$user[password]"; ?> Link to comment https://forums.phpfreaks.com/topic/116517-functions/#findComment-599117 Share on other sites More sharing options...
RCS Posted July 25, 2008 Author Share Posted July 25, 2008 Can anyone give me an example?? I would really like to write my code this way and I'm not sure how I would make the my queries... Link to comment https://forums.phpfreaks.com/topic/116517-functions/#findComment-599124 Share on other sites More sharing options...
blueman378 Posted July 25, 2008 Share Posted July 25, 2008 its kind of a pointless endevour, its basically the same amount of code to use your function as it is to write out the initial query, eg: $checkuser = mysql_num_rows($uinfo); would simply be wrote as $checkuser = dbNumRows($uinfo); all your basically doing with this is renaming the functions built into php, this: $udata = mysql_fetch_array($uinfo); would be: $udata = dbFetchArray($uinfo); kinda pointless, if you wrote a function eg to run the query for you that might be worthwhile, like one that simply accepts the where statment or something, although still not really worth it Link to comment https://forums.phpfreaks.com/topic/116517-functions/#findComment-599126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.