PHP+MySQL Database Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raskanally
    165th Top Average Player
    • Feb 2007
    • 292

    #1

    PHP+MySQL Database Problem

    Ill try to give a short account of what i need help with.

    Im currently in the process of building my own forums from scratch using PHP+MySQL.
    I have the signup.php page and the create_cat.php page. I also have the connect.php which is used to connect to the MySQL database when every page loads.
    But for some reason it will not let me select which database I want to use.
    Every page loads fine untill i tell it to include the connect.php.
    Code:
    <?php  
    //connect.php
    $server = 'localhost';  
    $username   = 'root';  
    $password   = 'salamander';  
    $database   = 'website';  
      
    if(!mysql_connect($server, $username,  $password, $database))  
    {  
        exit('Error: could not establish database connection');  
    }
    ?>
    im using PHP 5 and MySQL 5.5.
    i also created a test page to see if it could connect to just the main MySQL database using this syntax:
    Code:
    <?php
    $link = mysql_connect(
       'localhost', 
       'root', 
       'salamander', 
       'website'
    );
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_close($link);
    ?>
    And that connects fine.
    Im also including a copy of the signup.php page just incase the mysql part of that form could have errors i am not seeing.
    Code:
    <?php  
    //signup.php  
    include 'connect.php';  
    include 'header.php';  
      
    echo '<h3>Sign up</h3>';  
      
    if($_SERVER['REQUEST_METHOD'] != 'POST')  
    {  
        echo '<form method="post" action="">  
            Username: <input type="text" name="user_name" /><br />  
            Password: <input type="password" name="user_pass"><br /> 
            Password again: <input type="password" name="user_pass_check"><br /> 
            E-mail: <input type="email" name="user_email"><br /> 
            <input type="submit" value="Register" /><br /> 
         </form>'; 
    } 
    else 
    {  
        $errors = array();
      
        if(isset($_POST['user_name']))  
        {  
            if(!ctype_alnum($_POST['user_name']))  
            {  
                $errors[] = 'The username can only contain letters and digits.';  
            }  
            if(strlen($_POST['user_name']) > 30)  
            {  
                $errors[] = 'The username cannot be longer than 30 characters.';  
            }  
        }  
        else  
        {  
            $errors[] = 'The username field must not be empty.';  
        }  
      
        if(isset($_POST['user_pass']))  
        {  
            if($_POST['user_pass'] != $_POST['user_pass_check'])  
            {  
                $errors[] = 'The two passwords did not match.';  
            }  
        }  
        else  
        {  
            $errors[] = 'The password field cannot be empty.';  
        }  
      
        if(!empty($errors))
        {  
            echo 'Uh-oh.. a couple of fields are not filled in correctly..'; 
            echo '<ul>'; 
            foreach($errors as $key => $value)
            { 
                echo '<li>' . $value . '</li>';
            } 
            echo '</ul>'; 
        } 
        else 
        { 
            $sql = "INSERT INTO 
                        users(user_name, user_pass, user_email ,user_date, user_level) 
                    VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', 
                           '" . sha1($_POST['user_pass']) . "', 
                           '" . mysql_real_escape_string($_POST['user_email']) . "', 
                            NOW(), 
                            0)";  
      
            $result = mysql_query($sql);  
            if(!$result)  
            {  
                echo 'Something went wrong while registering. Please try again later.'; 
            } 
            else 
            { 
                echo 'Successfully registered. You can now <a href="signin.php">sign in</a> and start posting! :-)'; 
            } 
        } 
    } 
     
    include 'footer.php';  
    ?>
    and it says it connects fine.
    anyone have an easy solution to this? (besides getting a pre-made forum)
    Im using Apache 2.2 as well.

    Still just Smashing away
  • emerald000
    the Mathemagician~
    • Nov 2005
    • 1320

    #2
    Re: PHP+MySQL Database Problem

    The database shouldn't be chosen in mysql_connect. The correct syntax would be:

    Code:
    if(!mysql_connect($server, $username, $password))  
    {  
        exit('Error: could not establish database connection');  
    }
    
    if(!mysql_select_db($database))  
    {  
        exit('Error: could not establish database connection');  
    }

    Comment

    • raskanally
      165th Top Average Player
      • Feb 2007
      • 292

      #3
      Re: PHP+MySQL Database Problem

      had that originally. i shall retry it and see if it works.

      Edit: odd. i honestly had that code when i initially started this task, and it didn't work.

      now it does. thank you very much for the assistance. it would have taken me a long time to revert back to that code string.
      Last edited by raskanally; 02-18-2012, 04:53 PM.

      Still just Smashing away

      Comment

      • fido123
        FFR Player
        • Sep 2005
        • 4245

        #4
        Re: PHP+MySQL Database Problem

        Might be off on the mysql_error(); part. If not just delete everything after 'or' on line 1

        Code:
        <?php
        mysql_connect('localhost', 'USERNAME', 'PASSWORD') or die (echo mysql_error());
        mysql_select_db('DATABASE_NAME') or echo "Database does not exist";
        ?>
        If this doesn't work I'm pretty sure the problem is in your database. If you could post the output of the following commands it would be great. To make a lot file just use \t FILENAME to start logging then \T to stop (might be the other way around):

        Code:
        SHOW DATABASES;
        USE (database you're trying to use);
        SHOW TABLES;
        Last edited by fido123; 02-18-2012, 07:29 PM.

        Comment

        • aperson
          FFR Hall of Fame
          FFR Simfile Author
          • Jul 2003
          • 3431

          #5
          Re: PHP+MySQL Database Problem

          dont ever connect to databases as the root mysql user in an application script; not even in development.

          create a database user that has grants on the specific databases you need to work with and connect as that user:

          GRANT ALL ON `website`.* TO 'dbuser'@'localhost' IDENTIFIED BY 'salamander';


          you should also consider using the mysqli libraries (mysqli_connect, mysqli_pconnect) instead. same usage, better performance. you likely already have the packages installed.

          Comment

          • fido123
            FFR Player
            • Sep 2005
            • 4245

            #6
            Re: PHP+MySQL Database Problem

            What aperson is saying is the truth. I usually create an account called dev that can create tables and do everything neccesary to create a DB and an account called web that can just use:

            SELECT
            UPDATE
            INSERT
            and if needed DELETE


            MySQLi is the new standard of doing things although not really needed. My work doesn't use it but it makes things much easier and you don't need to worry about sanatizing all your database input with mysql_real_escape_string().
            Last edited by fido123; 02-19-2012, 12:24 PM.

            Comment

            • raskanally
              165th Top Average Player
              • Feb 2007
              • 292

              #7
              Re: PHP+MySQL Database Problem

              Thanks for the input guys.
              I am 100% new to this so root is just what was there.
              I will be creating the new accounts later tonight and shall press forward in this learning process.

              Still just Smashing away

              Comment

              • dAnceguy117
                new hand moves = dab
                FFR Simfile Author
                • Dec 2002
                • 10097

                #8
                Re: PHP+MySQL Database Problem

                this thread = good read. I just started working with MySQL a little bit via Java, so it's interesting to see some of the same issues come up from a different (and more common) perspective. Can't wait to start learning some PHP later this semester.

                Comment

                Working...