Jump to content

Functions


RCS

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.