Go Back   Flash Flash Revolution > General Discussion > Technology
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 02-18-2012, 11:21 AM   #1
raskanally
165th Top Average Player
FFR Veteran
 
raskanally's Avatar
 
Join Date: Feb 2007
Location: Regional Rank: 18th
Age: 35
Posts: 292
Send a message via AIM to raskanally Send a message via MSN to raskanally Send a message via Yahoo to raskanally Send a message via Skype™ to raskanally
Question 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
raskanally is offline   Reply With Quote
Old 02-18-2012, 12:50 PM   #2
emerald000
the Mathemagician~
Retired StaffFFR Veteran
 
emerald000's Avatar
 
Join Date: Nov 2005
Location: Quebec City
Age: 33
Posts: 1,320
Send a message via Skype™ to emerald000
Default 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');  
}
emerald000 is offline   Reply With Quote
Old 02-18-2012, 05:30 PM   #3
raskanally
165th Top Average Player
FFR Veteran
 
raskanally's Avatar
 
Join Date: Feb 2007
Location: Regional Rank: 18th
Age: 35
Posts: 292
Send a message via AIM to raskanally Send a message via MSN to raskanally Send a message via Yahoo to raskanally Send a message via Skype™ to raskanally
Default 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.
__________________

Still just Smashing away

Last edited by raskanally; 02-18-2012 at 05:53 PM..
raskanally is offline   Reply With Quote
Old 02-18-2012, 08:23 PM   #4
fido123
FFR Player
 
fido123's Avatar
 
Join Date: Sep 2005
Age: 32
Posts: 4,245
Default 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 at 08:29 PM..
fido123 is offline   Reply With Quote
Old 02-19-2012, 12:45 AM   #5
aperson
FFR Hall of Fame
Retired StaffFFR Simfile AuthorFFR Veteran
 
aperson's Avatar
 
Join Date: Jul 2003
Location: Houston
Posts: 3,428
Send a message via AIM to aperson
Default 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.
__________________

aperson is offline   Reply With Quote
Old 02-19-2012, 01:22 PM   #6
fido123
FFR Player
 
fido123's Avatar
 
Join Date: Sep 2005
Age: 32
Posts: 4,245
Default 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 at 01:24 PM..
fido123 is offline   Reply With Quote
Old 02-19-2012, 01:41 PM   #7
raskanally
165th Top Average Player
FFR Veteran
 
raskanally's Avatar
 
Join Date: Feb 2007
Location: Regional Rank: 18th
Age: 35
Posts: 292
Send a message via AIM to raskanally Send a message via MSN to raskanally Send a message via Yahoo to raskanally Send a message via Skype™ to raskanally
Default 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
raskanally is offline   Reply With Quote
Old 03-3-2012, 12:28 AM   #8
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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.
dAnceguy117 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 01:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution