Reputation: 27
I do not know why it won't connect I have no ideas. When I try to connect it also tells me it's successfully connected to the mysql its weird please help.
It's been annoying the crap out of me and I really need it to work soon because it is driving me nuts.
<?
if(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$email = mysql_real_escape_string($_POST['email']);
$checkusername = mysql_query("SELECT * FROM users WHERE Username = '".$username."'");
if(mysql_num_rows($checkusername) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that username is taken. Please go back and try again.</p>";
$checkemail = mysql_query("SELECT * FROM users WHERE email = '".$email."'");
if(mysql_num_rows($checkemail) == 1)
{
echo "<h1>Error</h1>";
echo "<p>Sorry, that email is taken. Please go back and try again.</p>";
}
}
else
{
$registerquery = mysql_query("INSERT INTO users (Username, Password, EmailAddress) VALUES('".$username."', '".$password."', '".$email."')");
if($registerquery)
{
echo "<h1>Success</h1>";
echo "<p>Your account was successfully created. Please <a href=\"login.php\">click here to login</a>.</p>";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your registration failed. Please go back and try again.</p>";
}
}
}
else
{
?>
<h1>Register</h1>
<p>Please enter your details below to register.</p>
<form method="post" action="register.php" name="registerform" id="registerform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<label for="email">Email Address:</label><input type="text" name="email" id="email" /><br />
<input type="submit" name="register" id="register" value="Register" />
</fieldset>
</form>
<?php
}
?>
Mysql Connected Successfully
Warning: mysql_real_escape_string(): Access denied for user 'UNKNOWN_USER'@'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 28
Warning: mysql_real_escape_string(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 28
Warning: mysql_real_escape_string(): Access denied for user 'UNKNOWN_USER'@'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 29
Warning: mysql_real_escape_string(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 29
Warning: mysql_real_escape_string(): Access denied for user 'UNKNOWN_USER'@'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 30
Warning: mysql_real_escape_string(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 30
Warning: mysql_query(): Access denied for user 'UNKNOWN_USER'@'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 32
Warning: mysql_query(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 32
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/parap00per/public_html/register.php on line 34
Warning: mysql_query(): Access denied for user 'UNKNOWN_USER'@'localhost' (using password: NO) in /home/parap00per/public_html/register.php on line 48
Warning: mysql_query(): A link to the server could not be established in /home/parap00per/public_html/register.php on line 48
Error
Sorry, your registration failed. Please go back and try again.
Upvotes: 0
Views: 417
Reputation: 74219
You're not selecting a database with mysql_select_db()
.
(as per your original posted code which was edited).
Replace the xxx
with your own information.
$host="localhost"; // Host name.
$db_user="xxx"; // MySQL username.
$db_password="xxx"; // MySQL password.
$database="xxx"; // Database name.
$link = mysql_connect($host,$db_user,$db_password);
if (!$link) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t use the DB : ' . mysql_error());
}
You're also closing your connection prematurely using mysql_close($link);
place it after you've finished querying, or don't use it at all. Your connection will close anyway once it's finished executing.
Edit:
I noticed you're using EmailAddress
and email
as column names. One of them or others may not be correct.
Add error reporting to the top of your file(s) which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also, use mysqli_*
with prepared statements, or PDO with prepared statements.
Footnotes:
mysql_*
functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.
Upvotes: 2