02-18-2012, 11:21 AM | #1 |
165th Top Average Player
|
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'); } ?> 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); ?> 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'; ?> anyone have an easy solution to this? (besides getting a pre-made forum) Im using Apache 2.2 as well.
__________________
Still just Smashing away |
02-18-2012, 12:50 PM | #2 |
the Mathemagician~
|
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'); } |
02-18-2012, 05:30 PM | #3 |
165th Top Average Player
|
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.. |
02-18-2012, 08:23 PM | #4 |
FFR Player
Join Date: Sep 2005
Age: 32
Posts: 4,245
|
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"; ?> Code:
SHOW DATABASES; USE (database you're trying to use); SHOW TABLES; Last edited by fido123; 02-18-2012 at 08:29 PM.. |
02-19-2012, 12:45 AM | #5 |
FFR Hall of Fame
|
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.
__________________
|
02-19-2012, 01:22 PM | #6 |
FFR Player
Join Date: Sep 2005
Age: 32
Posts: 4,245
|
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.. |
02-19-2012, 01:41 PM | #7 |
165th Top Average Player
|
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 |
03-3-2012, 12:28 AM | #8 |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
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.
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|