MySQL/php: I created database and tables, when I try an INSERT I have #1146

386 Views Asked by At

I'm writing an installation script in php, this script has to ask the user some data and then store them in a XML file and on a database.

Here is the procedure:

1) Insert data;

2) Save configuration data (such as database name,user,password,host,port) in a XML file

3) Using those data to install a database (I have an empty dump I use to create it)

4) Insert in the "admin" table the first user data (taken from the form in point 1)

Now, everything works fine until point 4. When I try to access the database MySql always returns my an error #1146 saying that the table doesn't not exists.

This is my custom JSON log:

{
 "Status":"KO",
 "Reason":"SELECT * FROM bbscp_admin_user; || Table 'adb.bbscp_admin_user' doesn't exist",
 "Errno":1146
}

Obviously I checked both with phpMyAdmin and mysql (from cli) and I can say that both DB and Table DO exists.

Here is the part where I create the DB: [this works fine]

$Install = Install::getInstance();
$sqlscript = str_replace("__DBNAME__", 
                         $config_info['dbname'], 
                         file_get_contents('../config/bbscp-base-db.sql')
                        );

$connection = mysqli_connect($config_info['dbhost'],
                             $config_info['dbuser'],
                             $config_info['dbpwd']
                             ) or die("Unable to connect! Error: ".mysql_error());  

if(mysqli_query($connection,'CREATE DATABASE '.$config_info['dbname'].';')){
    echo 'Database successfully createad';
    mysqli_close($connection);  
}else{
    die('ERROR CREATING DATABASE!');
}

Here the part where I populate the database from the dump (it only add the tables)

$connection = mysqli_connect($config_info['dbhost'],
                             $config_info['dbuser'],
                             $config_info['dbpwd'],
                             $config_info['dbname']
                            ) or die("Unable to connect! Error: ".mysql_error());

if(mysqli_multi_query($connection, $sqlscript)){
    echo 'Database successfully imported';
    mysqli_close($connection);  
}else{
    die('ERROR IMPORTING DATABASE');
}

Now the decisive part, I open a connection with the database (using a library that I've developed in the past years and has always worked fine)

$DB = new Database(Install::getInstance());
$Q = new Query();

$DB->connect();
$query = $Q->addNewUser($config_info['nickname'],
                        $config_info['password'],
                        $config_info['firstname'],
                        $config_info['lastname'],
                        $config_info['email'],
                        0);  // this just returns the query string I need
       // the query in this case is:
       //   INSERT INTO bbscp_admin_user               
       //            (nickname,password,firstname,lastname,mail,confirmed)
       //          VALUES (the ones above);"



    $res=$DB->startQuery($query); // THIS IS WHERE THE ERROR IS RETURNED
           // this function only wraps the mysqli_query and returns a custom JSON

I've tried a lot of solution.. The tables DO exists but every query I try to invoke at this point doesn't work because apparently "table doesn't exist" (even using mysqli function instead of mines).

Thanks in advance and sorry for the long question! :-)

EDIT 1 During this days I've changed a lot of code, I tried using different implementation using functions, objects, redirects but none of them seems to work.

The thing is always the same:

  • It creates the database
  • Returns the error code when trying to add the user
  • If I refresh the page it correctly adds the user
0

There are 0 best solutions below