function username_exists($username){
                            global $db;
                            $sql = "SELECT 'username' FROM users WHERE 'username' = '$username'";
                            $result = mysqli_query($db, $sql);
                            confirmquery( $result );

                             if (mysqli_num_rows($result) > 0) {
                               return true;
                             }else {
                               return false;
                             }
                        }
                        if(username_exists($username)){
                                $error['username'] = 'Username already exists';
                              }

The code as shown is pretty straightforward, and should work. But in order to work it needs to be part of a bigger system, which you don't show.

  1. The variable $db is declared "global" meaning it's defined somewhere else. For the code to work $db should be made a connection to the database, which means there is a statement somewhere that says something like

    "$db = mysqli_connect("localhost", "username", "", "mysqli_examples");.

  2. Once the database is successfully connected, you can run a query against it.

You might want to look at one of the mysqli tutorials out there, such as:
https://www.codewall.co.uk/php-mysqli-examples-the-ultimate-tutorial/

good luck
jt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.