Jump to content

[SOLVED] PHP error: No Database Selected


rEhSi_123

Recommended Posts

I hope somebody could help me out! :-[

 

I have created a forum system and everything was running smooth untill i did some test runs.......

 

Now, whenever a user goes to register, fills in the fields and clicks on Register button an error is displayed on the screen saying 'no database selected'

 

I am pretty much sure that I have my connections all setup properly.....but still not able to find the problem.

 

Could somebody please care to have a look....

 

Thanks

 

Here is my code

 

register.php

<html>
<center>
<div id="holder">

<div id="userinfo">
<link rel="stylesheet" type="text/css" href="./style.css">
<?php

error_reporting(E_ALL ^ E_NOTICE);//Report all error except NOTICES

include_once "functions.php";

connect();


if ( empty( $_POST ) ){
    echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
    echo "<form method=\"post\" action=\"register.php\">\n";
    echo "<tr><td class=\"user_login\" colspan=\"2\" align=\"center\">Registration Form</td></tr>\n";
    echo "<tr><td class=\"user_login\">Username</td><td><input type=\"text\" name=\"username\" style=\"background-color:#00FFFF;\"></td></tr>\n";
    echo "<tr><td class=\"user_login\">Password</td><td><input type=\"password\" name=\"password\" style=\"background-color:#C0D9D9;\"></td></tr>\n";
    echo "<tr><td class=\"user_login\">Confirm Password</td><td><input type=\"password\" name=\"passconf\" style=\"background-color:#C0D9D9;\"></td></tr>\n";
    echo "<tr><td class=\"user_login\">E-Mail</td><td><input type=\"text\" name=\"email\" style=\"background-color:#C0D9D9;\"></td></tr>\n";
    echo "<tr><td class=\"user_login\">Name</td><td><input type=\"text\" name=\"name\" style=\"background-color:#C0D9D9;\"></td></tr>\n";
    echo "<tr><td class=\"user_login\" colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Register\"></td></tr>\n";
    echo "</form></table>\n";
}else {
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);
    $confirm = protect($_POST['passconf']);
    $email = protect($_POST['email']);
    $name = protect($_POST['name']);
    
    
    $errors = array();
    
        if(!$username){
            $errors[] = "Username is not defined!";
        }
        
        if(!$password){
            $errors[] = "Password is not defined!";
        }
        
        if($password){
            if(!$confirm){
                $errors[] = "Confirmation password is not defined!";
            }
        }
        
        if(!$email){
            $errors[] = "E-mail is not defined!";
        }
        
        if(!$name){
            $errors[] = "Name is not defined!";
        }
            
        if($username){
            if(!ctype_alnum($username)){
                $errors[] = "Username can only contain numbers and letters!";
            }
            
            $range = range(1,32);
            if(!in_array(strlen($username),$range)){
                $errors[] = "Username must be between 1 and 32 characters!";
            }
        }
        
        if($password && $confirm){
            if($password != $confirm){
                $errors[] = "Passwords do not match!";
            }
        }
        
        if($email){
            $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
            if(!preg_match($checkemail, $email)){
                $errors[] = "E-mail is not valid, must be name@server.tld!";
            }
        }
        
        if($name){
            $range2 = range(3,64);
            if(!in_array(strlen($name),$range2)){
                $errors[] = "Your name must be between 3 and 64 characters!";
            }
        }
              
        if($username){
            $sql = "SELECT * FROM `users` WHERE `username`='".$username."'";
            $res = mysql_query($sql) or die(mysql_error());
            
                if(mysql_num_rows($res) > 0){
                    $errors[] = "The username you supplied is already in use!";
                }
        }
        
        if($email){
            $sql2 = "SELECT * FROM `users` WHERE `email`='".$email."'";
            $res2 = mysql_query($sql2) or die(mysql_error());
            
                if(mysql_num_rows($res2) > 0){
                    $errors[] = "The e-mail address you supplied is already in use of another user!";
                }
        }
           
        if(count($errors) > 0){
            foreach($errors AS $error){
                echo $error . "<br>\n";
            }
        }else {
            $sql4 = "INSERT INTO `users`
                    (`username`,`password`,`email`,`name`,`admin`,`time`)
                    VALUES ('".$username."','".md5($password)."','".$email."','".$name."','0','".$time()."')";
            $res4 = mysql_query($sql4) or die(mysql_error());
            echo "You have successfully registered with the username <b>".$username."</b> and the password of <b>".$password."</b>!";
        }
}

?>

</div>
</div>
</center>
</html>

 

my connection settings


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="forum"; // Database name
//$tbl_name="forum_question"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


function connect(){
    $con = mysql_connect("localhost", "root", "") or die(mysql_error());
    $db = mysql_select_db("users", $con);
}

 

 

Link to comment
Share on other sites

Just get rid of your connect function (or do not call it). It's connecting to the db in a function and doing nothing with the results. I'm guessing this is overwriting the connection you've just created above it.

 

I removed the connect function and got these errors :-[

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\login\functions.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\wamp\www\login\functions.php on line 4
Username is not defined!
Password is not defined!
E-mail is not defined!
Name is not defined!

Link to comment
Share on other sites

Soory for double post.....

 

line 4 is basically this

 

$string = addslashes($string);

 

function protect($string){
    $string = mysql_real_escape_string($string);
    $string = strip_tags($string);
    $string = addslashes($string);
    
    return $string;
}

Link to comment
Share on other sites

When does this code get included? Is it in functions?

 

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

Link to comment
Share on other sites

When does this code get included? Is it in functions?

 

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

hmmmm.....The above code is actually in my globals.php

 

Link to comment
Share on other sites

When does this code get included? Is it in functions?

 

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

Right finally problem resolved...... ;D

 

Now, all my connection settings are in globals.php but I had few functions stored in this file which were also same as in functions.php.

 

So if I included both files then I would get an error saying

cannot redeclare mss(): function as it is previously define in ...'.php

 

The reason for technique is I am using them elsewhere in my php code.......

 

therefore now, I have included the connection settings in my register.php which seems to do the trick

 

Anways thanks for your help 'soak'

 

Once more thing, would you suggest any other technique instead of renaming my functions..... :-[

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.