rEhSi_123 Posted April 20, 2009 Share Posted April 20, 2009 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 [email protected]!"; } } 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 https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/ Share on other sites More sharing options...
soak Posted April 20, 2009 Share Posted April 20, 2009 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. Link to comment https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815040 Share on other sites More sharing options...
rEhSi_123 Posted April 20, 2009 Author Share Posted April 20, 2009 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 https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815045 Share on other sites More sharing options...
soak Posted April 20, 2009 Share Posted April 20, 2009 Have you set the correct details here: $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="forum"; // Database name Link to comment https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815048 Share on other sites More sharing options...
rEhSi_123 Posted April 20, 2009 Author Share Posted April 20, 2009 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 https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815049 Share on other sites More sharing options...
rEhSi_123 Posted April 20, 2009 Author Share Posted April 20, 2009 Have you set the correct details here: $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="forum"; // Database name Yes mate all details are correct....... Link to comment https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815051 Share on other sites More sharing options...
soak Posted April 20, 2009 Share Posted April 20, 2009 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 https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815058 Share on other sites More sharing options...
rEhSi_123 Posted April 20, 2009 Author Share Posted April 20, 2009 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 https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815065 Share on other sites More sharing options...
rEhSi_123 Posted April 20, 2009 Author Share Posted April 20, 2009 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...... 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 https://forums.phpfreaks.com/topic/154947-solved-php-error-no-database-selected/#findComment-815078 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.