Jump to content

Query Function? Good Idea or Bad


tsilenzio

Recommended Posts

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!

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

  • 2 weeks later...

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 = './../../../';

Link to comment
Share on other sites

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! 

Link to comment
Share on other sites

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 :P

 

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.