Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bobdebower's avatar

Database: Query Builder

Hey guys i'am trying to update my db_name but to prevent error's i'am trying Database: Query Builder

i'am lost can someone help me please? Thank you!

  public function handle()
    {

      $data = DB::table('users')
        ->select('id','name')
        ->where('db_name', null)
        ->limit(10);
//            ->update(['db_name' => DB::raw("CONCAT(name, '_', id)")]);// i normally use it here

 foreach ( $data as $user ) {
     $name = preg_replace("/[^a-zA-Z0-9]/", "", $user->name);
     $name = substr($name, 0, 8); 
     $new_dbname = $name  + $user->id;

         }

// IF NOT EXISTS!!  i'am getting a lot of error here.
$db = CREATE NEW DB IF NOT EXISTS::$new_dbname;
if ( $db->errors !== true ) { 
    DB::update('db_name', $new_dbname )->where('id',$user->id)->isnull('db_name');
  }

 }
0 likes
13 replies
bobdebower's avatar

Does anyone knows, how to create a new DB if not exist? is it possible? i now you can create a Comand for it using the command line but i want to. integrate it in my code above. like:

// de IF NOT EXISTS is wel belangrijk - anders krijg je een fout
$db = CREATE NEW DB IF NOT EXISTS::$new_dbname

Can someone please help.

automica's avatar

@bobdebower is this a one time thing?

if so, why not just do this via mysql on terminal or PHPMyAdmin?

bobdebower's avatar

If it's not possible i will stop working on it. I already created a createDatabaseCommand. makes it possible to create a database using the command line. but i want to be able to create a DB if not Exist and update my colmn . Thank you

automica's avatar

@bobdebower is this the same problem you had when you were trying to create site where each user had their own DB?

bobdebower's avatar

no, i already solved that. i just want to be able to create a DB if it doesn't exist. with a script code wittout creating it with the command line or phpmyadmin

bobdebower's avatar

i found this

  public function index() {
   $userName = 'bigboytr';  // Your Database name to be created
   DB::statement("CREATE DATABASE $userName");
  }

but it's in the controller. Can i implement it like below? below

// IF NOT EXISTS!!  i'am getting a lot of error here.
      $db = CREATE NEW DB IF NOT EXISTS::$new_dbname;
     if ( $db->errors !== true ) { 
   DB::update('db_name', $new_dbname )->where('id',$user->id)->isnull('db_name');
  }

}
automica's avatar

@bobdebower you'll probably want to use DB:statement()

eg

$db = DB::statement("CREATE DATABASE IF NOT EXISTS $new_dbname;");
1 like
bobdebower's avatar

Thank for your respond, sorry i have to bother you, but can you spot what i'am doing wrong? because unfortunately is not working. Thank you!

public function handle()
  {
    $data = DB::table('users')
        ->select('id','name')
        ->where('db_name', null)
        ->limit(10);

   foreach ( $data as $user ) {
     $name = preg_replace("/[^a-zA-Z0-9]/", "", $user->name);
     $name = substr($name, 0, 8); 

     $new_dbname = $name  + $user->id;

         }
// IF NOT EXISTS!!

    $db = DB::statement("CREATE DATABASE IF NOT EXISTS $new_dbname;");
if ( $db !== null ) {
    DB::table('users')->whereNull('db_name',$new_dbname)->update([
        'db_name' => DB::raw("CONCAT(name, '_', id)")
    ]);
}

}

}

bobdebower's avatar

Looks like something is going wrong, but i can't figure it out.

Please or to participate in this conversation.