Ensuring the creation of databases

In particular when you move your experiments from one server to another it is a great help if you do not have to worry about the presence of the tables in your MySQL databases. The following function assumes that the wwwrun user has the right to create tables.

function db_check_crea($table,$create) {
    if (! mysql_query ("SELECT COUNT(*) FROM $table")) {
        mysql_query ("CREATE TABLE $table ($create)");
        $err1=mysql_error ();
        if (! mysql_query ("SELECT COUNT(*) FROM people")) {
            echo "When I tried to create table $table
            the following error occured: <BR>
        echo "Table <CODE>$table</CODE> was created<BR>";
        return 1;
    return 0;

Once you have defined this function you can, at the beginning of your PHP program, call this function with the create definitions of all your tables. The script will take care of the creation of tables if necessary.

Oliver Kirchkamp 2013-04-15