1

I have set up a MySql database containing several tables (needed for uploading tons of data that is entered in excel). I would like to use each table name as a variables that automatically updates each time a new table is added to the database. However, I don't know how to prevent PHP from overwriting my $tableName variable in loops. This is what I got so far:

        <?php

            if (!mysql_connect($host, $username, $password)) {
                echo 'Could not connect to mysql';
                exit;
            }

            $sql = "SHOW TABLES FROM $database";
            $result = mysql_query($sql);

            if (!$result) {
                echo "DB Error, could not list tables\n";
                echo 'MySQL Error: ' . mysql_error();
                exit;
            }

            while ($row = mysql_fetch_row($result)) {
                $tableName = $row[0];
                include('../design.php');
            }


        ?>

In other words: On the frontpage of the website I would like to include data from every single database table. I have organized a framework for extracting that data and presenting it neatly in design.php. However, this file makes use of the $tableName variable for extracting the data and I think that variable is being overwritten at each loop causing the error (only the number one table will be displayed).

Instead, if I just separate the code in their own php objects, then everything works just fine:

        <?php
            $tableName = tablename1;
            include('../design.php');
        ?>
        <?php
            $tableName = tablename2;
            include('../design.php');
        ?>
        etc..

Can anyone explain to me how this works? Or if there is some smarter way of doing things?

Thanks in advance!

5
  • "I would like to use each table name as a variables that automatically updates each time a new table is added to the database." - Why not just use a trigger? Commented Oct 30, 2015 at 18:56
  • 3
    This is a pretty bad way to do things (with include). Whatever you have in design.php, turn it into a function that uses the tableName as a parameter and only include the file once at the top. I suspect your design.php file uses $row or $result which would account for your code breaking. Commented Oct 30, 2015 at 19:06
  • Try printing out $tableName right before each include, and see what happens. Commented Oct 30, 2015 at 19:07
  • You should include design.php outside the while loop What's inside design.php ? Commented Oct 30, 2015 at 19:17
  • Put db names in array and create function and use it in foreach(for example) instead of including some file also dont use mysql_* function they are deprecated Commented Oct 30, 2015 at 19:27

2 Answers 2

1

include the design.php file at the beginning of the script. Inside this file, define a function.

function doSomethingWithTableName ($table)
{
    // do something with it
}

Then, in your frontend file, you would do this:

$sql = "...";
$result = mysql_query ($sql);
while ($row = mysql_fetch_row($result))
{
    $processed_data = doSomethingWithTableName ($row[0]);
    // do something with $processed_data
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

I've no idea what's inside design.php but instead of including it inside the loop I'd create a function, something like:

function designFunction($tableName){
//put the design.php here
return $something;
}

Now use the designFunction inside your loop

while ($row = mysql_fetch_row($result)) {
  $tableName = $row[0];
  $someResult = designFunction($tableName);
  //etc...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.