SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: My 4 functions

  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post My 4 functions

    Hi,

    After working a while with database in php I have made my own 4 functions which I think are much easier to use than to create queries again and again in the project, but the functions perform only basic functions and do not go in depth. My question is that maybe some php expert can look into this and maybe optimize them so they do not do some sort of conflict etc. ? Hope you get my point and if you like it then you are free to use it also.

    Code:
    <?php
    function sqlselect($tblname, $fields, $whrfilds, $whrconds, $whrvalus, $orderby, $limit)
    {
            if (empty($fields)) { $fields = "*"; }
    
            if (!empty($whrfilds))
            {
                    for ($sqs = 0; $sqs <= count($whrfilds) - 1; $sqs++)
                    {
                            $whrdata[] = trim($whrfilds[$sqs]) . " " . trim($whrconds[$sqs]) . " " . trim($whrvalus[$sqs]);
                    }
                    $whrdata = " WHERE " . implode (" AND ", $whrdata);
            }
    
            if (!empty($orderby)) { $odrdata = " ORDER BY " . $orderby; }
            if (!empty($limit)) { $lmtdata = " LIMIT " . $limit; }
    
            $sql = sprintf ("SELECT %s FROM %s%s%s%s", $fields, $tblname, $whrdata, $odrdata, $lmtdata);
            $res = mysql_query($sql);
            
            if ($res)
            {
                    if (mysql_num_rows($res) <> 0)
                    {
                            while ($ado = mysql_fetch_array($res)) { $retdata[] = $ado; }
                    }
                    if (!empty($retdata)) { return $retdata; } else { return false; }
            }
            else
            {
                    if ($settings["debug"] == "0") { return false; } else { return mysql_error(); }
            }
    }
    
    function sqlinsert ($tblname, $fields, $values)
    {
            for ($sqi = 0; $sqi <= count($fields) - 1; $sqi++)
            {
                    $tbldata[] = $fields[$sqi] . " = " . fix($values[$sqi]);
            }
    
            $tbldata = implode (", ", $tbldata);
    
            $sql = sprintf ("INSERT INTO %s SET %s", $tblname, $tbldata);
            $res = mysql_query($sql);
    
            if (!$res)
            {
                    if ($settings["debug"] == "0") { return false; } else { return mysql_error(); }
            }
            else { return mysql_insert_id(); }
    }
    
    function sqlupdate ($tblname, $fields, $values, $whrfilds, $whrconds, $whrvalus, $limit, $skip)
    {
            for ($squd = 0; $squd <= count($fields) - 1; $squd++)
            {
                    if (!empty($skip))
                    {
                            $doskip = 0;
    
                            for ($squs = 0; $squs <= count($skip) - 1; $squs++)
                            {
                                    if ($skip[$squs] == $fields[$squd]) { $doskip++; }
                            }
    
                            if (empty($doskip)) { $tbldata[] = $fields[$squd] . " = " . fix($values[$squd]); }
                    }
            }
            $tbldata = implode (", ", $tbldata);
    
            if (!empty($whrfilds))
            {
                    for ($sqs = 0; $sqs <= count($whrfilds) - 1; $sqs++)
                    {
                            $whrdata[] = trim($whrfilds[$sqs]) . " " . trim($whrconds[$sqs]) . " " . trim($whrvalus[$sqs]);
                    }
                    $whrdata = " WHERE " . implode (" AND ", $whrdata);
            }
    
            if (!empty($limit)) { $lmtdata = " LIMIT " . $limit; }
    
    
            $sql = sprintf ("UPDATE %s SET %s%s%s", $tblname, $tbldata, $whrdata, $lmtdata);
            $res = mysql_query($sql);
            
            if (!$res)
            {
                    if ($settings["debug"] == "0") { return false; } else { return mysql_error(); }
            }
            else { return true; }
    }
    
    function sqldelete ($tblname, $whrfilds, $whrconds, $whrvalus, $limit)
    {
            if (!empty($whrfilds))
            {
                    for ($sqs = 0; $sqs <= count($whrfilds) - 1; $sqs++)
                    {
                            $whrdata[] = trim($whrfilds[$sqs]) . " " . trim($whrconds[$sqs]) . " " . trim($whrvalus[$sqs]);
                    }
                    $whrdata = " WHERE " . implode (" AND ", $whrdata);
            }
            if (!empty($limit)) { $lmtdata = " LIMIT " . $limit; }
    
            $sql = sprintf ("DELETE FROM %s%s%s", $tblname, $whrdata, $lmtdata);
            $res = mysql_query($sql);
    
            if (!$res)
            {
                    if ($settings["debug"] == "0") { return false; } else { return mysql_error(); }
            }
            else { return true; }
    }
    ?>
    Thanks.

  2. #2
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Should I keep them as functions or turn this into a class ? I don't know maybe this is not good enough or noob enough but I am learning and find it quite easy than to write mysql_query etc. again and again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •