ThingEngineer / PHP-MySQLi-Database-Class

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.
Other
3.3k stars 1.34k forks source link

Using rawQuery for CREATE TABLE #1003

Closed boionfire81 closed 6 months ago

boionfire81 commented 2 years ago

Doesn't seem to work. It says error, but when using getLastErrno() it prints nothing

boionfire81 commented 2 years ago

Here's my code `echo "start";

    require_once('includes/db/MysqliDb.php');

    echo "<br>Included database driver<br>";

    echo $_POST['host']."<br>".$_POST['username']."<br>".$_POST['password']."<br>".$_POST['name']."<br>".$_POST['key'];
    $db = new MysqliDb (Array (
            'host' => $_POST['host'],
            'username' => $_POST['username'], 
            'password' => $_POST['password'],
            'db'=> $_POST['name']));

    if($db->rawQuery("CREATE TABLE `settings`(`setting_id` varchar(100) COLLATE utf8mb4_bin DEFAULT NULL,`setting_value` varchar(1000) COLLATE utf8mb4_bin DEFAULT NULL")){

        echo "success";

    }else{

        $error=$db->getLastError();
        echo $error;
        exit;

    }`

It echos my stuff. I verified the login was correct a couple times, and nothing prints up as to why the raw query doesn't work

darkcavalry commented 1 year ago

Im using this query for table creation maybe it's help for you.

         $db_pref = '';
        // * users Table
        if(!$db->tableExists($db_pref."users")){
            $db->rawQuery("
                            CREATE TABLE `".$db_pref."users` (
                            `user_id` int(11) NOT NULL AUTO_INCREMENT,
                            `user` varchar(100) DEFAULT NULL,
                            `email` varchar(100) NOT NULL,
                            `password` varchar(50) NOT NULL,
                            `user_type` tinyint(2) NOT NULL DEFAULT 0,
                            `user_active` tinyint(2) NOT NULL DEFAULT 0,
                            `user_primary` tinyint(2) NOT NULL DEFAULT 0,
                            `created_on` timestamp NOT NULL DEFAULT current_timestamp(),
                            `last_login` datetime DEFAULT NULL,
                            `login_ip` varchar(15) DEFAULT NULL,
                            `user_access` text DEFAULT NULL,
                            `user_folder` varchar(20) DEFAULT NULL,
                            `session_token` varchar(255) DEFAULT NULL,
                            `user_theme` varchar(50) DEFAULT NULL,
                            `api_key` varchar(50) DEFAULT NULL,
                            PRIMARY KEY (`user_id`)
                            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED;
            ");
            if($db->getLastErrno() !== 0){
                die('Table creation error<br>'.$db->getLastError());
            }
        }
ThingEngineer commented 6 months ago

It looks like the (working) example from @darkcavalry accomplishes what you want.

If not, utilize debugging tools like var_dump or print_r to inspect the value of $db->getLastError() after the query execution, also verify db permissions. And consider using prepared statements instead of raw queries for improved security in this situation.