If no result is found, $wpdb->get_row
will return NULL
not false
https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row
You are using add_action('init', 'gallerypage');
so it runs on every load. You should be using register_activation_hook
so that it only runs when the plugin is activated.
https://codex.wordpress.org/Function_Reference/register_activation_hook
Try using get_page_by_path()
to check if the page already exists. Then you won’t need to use the $wpdb
global and custom sql.
https://codex.wordpress.org/Function_Reference/get_page_by_path