SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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 />';
    ?>
    Last edited by SpacePhoenix; Apr 5, 2011 at 10:32. Reason: put php tags round php code to improve readability

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't forget to reload privileges
    my mobile portal
    ghiris.ro

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks i seem to have solved the problem


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •