Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:12:51+00:00 2026-06-03T03:12:51+00:00

What I need: An efficent SQL Script builder to replace the one below. The

  • 0

What I need:

An efficent SQL Script builder to replace the one below.

The background.

A really simple program but I need a more efficient way of producing it, the reason I am having this code is I need other developers to be able to create ‘modules’ on my platform and database tables, but I am blocking them having full access to the core database, they have to use my $api->database-> access layer to access their tables/data. it blocks any request to the core part of my program.

Below is how I expect to use the functions to build the SQL Create Table Script.
SqlField Class/function just returns a flat array of the values passed, nothing special.

//The structure of the params are..

//$fields[] = SqlField::create($f_name, $f_type, $null, $auto_inc, $is_pk, $is_unique);
$fields[] = SqlField::create('id', 'int(7)', 0, 1, 1, 1);

$sql = $sqlTable->createTable('MyTableName', $fields );

Below is the current function I wrote quickly, but it was getting messy quickly and isnt complete.

public function createTable($tableName, $fields = array()) {

    $sql="CREATE TABLE xmod_".$tableName." ( \r\n";
        $isinit = true;

        foreach($fields as $field) {

            $sql .= ($isinit)? "" : ", \r\n" ;

            $isinit = false;

            $sql .= "".$field[0]." ".$field[1]." ";

            if($field[2] == true) {
                $sql .= "NOT NULL ";
            }

            if($field[3] == true) {
                $sql .= "auto_increment ";
            }   



        }

        $isinit = true;

        //Work on the primary keys
        foreach($fields as $field) {



            if($field[4] == 1) {

                $sql .= ($isinit)? "" : ", \r\n" ;
                $isinit = false;

                $sql .= "PRIMARY KEY (".$field[0].")";
            }

        }

        $isinit = true;

        //Work on the unique fields
        foreach($fields as $field) {

            if($field[5] == 1) {

                $sql .= ($isinit)? "" : ", \r\n" ;
                $isinit = false;

                $sql .= "UNIQUE id (".$field[0].")";
            }


        }           

        $sql .= "\r\n )";


        return $sql;



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:12:51+00:00Added an answer on June 3, 2026 at 3:12 am

    Though I haven’t tried it myself CodeIgniter provides something similar. http://codeigniter.com/user_guide/database/forge.html

    (copied from comment)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write more efficient code in a C program, and I need
I need an efficient mechanism for detecting changes to the DOM. Preferably cross-browser, but
I need to replace many different sub-string in a string in the most efficient
The DataReader is more efficient than a DataTable if you only need to show
I need some help working out how to make my query more efficient. I'm
I am trying to become more efficient in my SQL programming. I am trying
I need a new perspective on how to design a reliable and efficient SQL
This is a really simple request, but I am not quite sure the easiest/most
I need this to be done in Oracle SQL (10gR2). But I guess, I
I am doing a project at the moment and I need an efficient method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.