Can't connect to mysql after granting privileges

this code is to create a new user based on posted variables. it works fine for creating the necessary tables and granting privileges. but then when i log out as root and try to log back in as the user it’s failing. why is it failing? Thanks a lot, G.


<?php
                extract( $_POST );
                include( '../php/userobject.php' );
                
                @ $db = mysqli_connect('localhost', 'root', 'xxxxxx', 'mealchamp');
                if ( mysqli_connect_errno() ) {
                    echo 'Error: could not connect to database as root.<br />';
                    exit;
                }
                else
                    echo 'Connected to database as root.<br />';

                $query = "SELECT username FROM userlist WHERE username = $uname";
                $result = mysqli_query($db, $query);

                if ($result) 
                    echo 'That user name is already taken.<br/>Please hit back on your browser, and try a different user name.<br/>';
                else {
                    echo 'That username is available.<br/>';
                    echo "$fname<br/>";
                    echo "$lname<br/>";
                    echo "$add1<br/>";
                    echo "$add2<br/>";
                    echo "$city<br/>";
                    echo "$prov<br/>";
                    echo "$ctry<br/>";
                    echo "$post<br/>";
                    echo "$email<br/>";
                    echo "$uname<br/>";
                    echo "$pass1<br/>";
                    echo "$sex<br/>";
                    echo "$birth<br/>";
                    echo "$height<br/>";
                    echo "$weight<br/>";
                    
                    $query = "INSERT INTO userlist 
                            (firstname, lastname, address1, address2, city,     province, country, postal, email,     username, password, sex,         birth,         height,         weight) VALUES
                            ('$fname',    '$lname',    '$add1',    '$add2',    '$city','$prov',    '$ctry', '$post','$email','$uname',    '$pass1',    '$sex', '$birth', '$height',     '$weight')";
                    $result = mysqli_query($db, $query);
                    if ($result)
                        echo "Userlist was updated successfully<br />";
                    else
                        echo "Could not update userlist";

                    $query = "CREATE TABLE ".$uname."_persfood  
                        (persfoodid     INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         food                     VARCHAR(30) NOT NULL)";
                    $result = mysqli_query($db, $query);
                    if ($result)
                        echo "Personal food table was created successfully<br />";
                    else
                        echo "Could not create personal food table<br />";

                    $query = "GRANT select ON mealchamp.userlist TO ".$uname." IDENTIFIED BY '$pass1'";
                    $result = mysqli_query($db, $query);
                    if ($result)
                        echo "User priveleges were granted successfully<br />";
                    else
                        echo "Could not grant user priveleges<br />";
                    $query = "GRANT select, insert, update, delete, index ON mealchamp.".$uname."_persfood TO ".$uname." IDENTIFIED BY '$pass1'";
                    $result = mysqli_query($db, $query);
                    if ($result)
                        echo "User priveleges were granted successfully<br />";
                    else
                        echo "Could not grant user priveleges<br />";
                    
                    mysqli_close($db);

                    @ $db = mysqli_connect('localhost', $uname, $pass1, 'mealchamp');
                    if ( mysqli_connect_errno() ) {
                        echo 'Error: could not connect to database as user.<br />';
                        exit;
                    }
                    else {
                        echo 'Connected to database as user.<br />';
?>

there is a missing } at the end but that was just a mistake in editing down the code to post it.

the script is failing in that the errno message is being generated at the end. If i try to do things with the $db resource afterward i get warnings the resource is not valid.

desperate! thanks, G

don’t forget to reload privileges

thanks i seem to have solved the problem