Jump to content

registration(no database selected)


Kapacity
 Share

Recommended Posts

<?phpif($_POST){if(!$errors){$name=$_POST["name"];$password=$_POST["password"];$email=$_POST["email"];include_once "functions.php";connect();$sql="SELECT * FROM user_information WHERE Name='$name'";$rst=mysql_query($sql) or die(mysql_error());echo $rst;};};if(!$_POST||$errors){?>The user registers to my site. It checks to see if the name is already used. If it is, tell the user that, etc.. Anyways, right now i have it looking for the name is the database-table. when it echo's the $rst, im given "no database selected".. i don't know, im just confused about this and any help would be awesome.

Link to comment
Share on other sites

<?phpif($_POST){if(!$errors){$name=$_POST["name"];$password=$_POST["password"];$email=$_POST["email"];include_once "functions.php";<====connect();<====$sql="SELECT * FROM user_information WHERE Name='$name'";$rst=mysql_query($sql) or die(mysql_error());echo $rst;};};if(!$_POST||$errors){?>actually if you look above where the arrows are, you will see that i am calling a script by the name functions.php. this script has the information for connecting to the database. btw, i did take that database connection script and embedded into this code here. didnt make a difference..

Link to comment
Share on other sites

Well, lets have a look at functions.php, maybe there is something wrong there.By the way if you declare a variable in the function connect(), it will not be accessable outside of the function.

Link to comment
Share on other sites

<?phpfunction protect($string){$string = mysql_real_escape_string($string);$string = addslashes($string);$string = strip_tags($string);return $string;};function connect(){$con = mysql_connect(localhost, root, "") or die(mysql_error());$db = mysql_select_db(dark_oblivion, $con);};?>this is the script for functions.php.. wait.. are you saying i might have 2 set it up like..<?phpfunction protect($string){$string = mysql_real_escape_string($string);$string = addslashes($string);$string = strip_tags($string);return $string;};function connect($db){$con = mysql_connect(localhost, root, "") or die(mysql_error());$db = mysql_select_db(dark_oblivion, $con);return $db;};?>or however it would be typed.. o_O

Link to comment
Share on other sites

the issue si that the variables you define in connect() are only those in the function itself. you would have to return the $con variable, and not $db;... changing the function to something like this would be a better thing:

funtion connect(){   $connection = mysql_connect('localhost','root','') or die(mysql_error());   @mysql_select_db('dark_oblivion',$connection) or die(mysql_error());   return $connection;}

Now change the original code you posted to this:

<?phpif($_POST){if(!$errors){$name=$_POST["name"];$password=$_POST["password"];$email=$_POST["email"];include_once "functions.php";$con = connect();$sql="SELECT * FROM user_information WHERE Name='$name'";$rst=mysql_query($sql,$con) or die(mysql_error());echo $rst;};};if(!$_POST||$errors){?>

return doesn't define a variable outside the function, it just gives you a value. you must put a variable in front of it($var =)

Link to comment
Share on other sites

function connect(){$con = mysql_connect(localhost, root, "") or die(mysql_error());mysql_select_db(dark_oblivion, $con) or die(mysql_error());return $con;};?>-------------------------------<?phpif($_POST){if(!$errors){$name=$_POST["name"];$password=$_POST["password"];$email=$_POST["email"];include_once "functions.php";$con = connect();$sql="SELECT * FROM user_information WHERE Name='$name'";$rst=mysql_query($sql,$con) or die(mysql_error());echo $rst;};};if(!$_POST||$errors){?>Alright, so the message that is shown is as follows. "Unknown database 'dark_oblivion'" ...Now, i used phpmyadmin and created a profile in the database. The information that was filled in the registration web page is the same. so based on the above code.. it should have located that specific row and echo'd the information no?PS: Some variables are undefined because they will be used once i figure this problem out. thanx for the help everyone! :)

Link to comment
Share on other sites

$con = mysql_connect(localhost, root, "") or die(mysql_error());mysql_select_db(dark_oblivion, $con) or die(mysql_error());Those are not undefined variables, PHP sees that as undefined constants. In PHP you don't just write a string of text. If you *do* want a string, then you put quotes around it. If you don't put quotes around it then PHP assumes it is a constant. I doubt you have defined constants called localhost, root, and dark_oblivion. You probably want to use the literal strings, so put quotes around them. Anyway, there's no excuse for doing this:

PS: Some variables are undefined because they will be used once i figure this problem out. thanx for the help everyone!
There's no excuse for having known errors in your software, in *any* software, ever. If you know about errors, fix them. Period.Now, about the SQL error. If it's telling you that it can't find a database with a certain name, that means that a database does not exist with that name. Check in phpMyAdmin to make sure you have created a database called "dark_oblivion" (you probably haven't or else you would see a different error). If you're new to this, databases and tables aren't the same thing, if you create a table called "dark_oblivion" it's not going to find that as a database.
Link to comment
Share on other sites

Listen buddy, don't get me wrong here. I do appreciate your help. however, i don't appreciate how you are talking to me. talking down in fact. now listen.. i only studied tutorials on php, sql, etc.. this is my first time messing around with them. what im trying to get at is i do have experience with scripting/programming. If anything.. i understand a lot of how things process, but the way some things are written mess me up. like how at the end of each php command you must place a semi colon.. anyways.. thanx a lot for your help :)PS!! I put quotations for localhost, root, and dark_oblvion.. same deal. and yes.. YES i did create the database and make sure the typing is correct. o_O

Link to comment
Share on other sites

Listen buddy, don't get me wrong here. I do appreciate your help. however, i don't appreciate how you are talking to me. talking down in fact. now listen.. i only studied tutorials on php, sql, etc.. this is my first time messing around with them. what im trying to get at is i do have experience with scripting/programming. If anything.. i understand a lot of how things process, but the way some things are written mess me up. like how at the end of each php command you must place a semi colon.. anyways.. thanx a lot for your help :) PS!! I put quotations for localhost, root, and dark_oblvion.. same deal. and yes.. YES i did create the database and make sure the typing is correct. o_O
Kapacity you need to realize that Justsomeguy has helped A LOT of people, look at his post count. I'm sure after how long he's been here he's lost some patience with people. These are common mistakes made with PHP, and very logical ones to be seen. To him(as well as with other experienced/"advanced") PHP programmers, these things are a more common sense mistake than anything else. It wears on all of our patience, trust me. When you get better, it will annoy you too(if you help others, that is). If you know Javascript, they're about the same. They're both C(++) based languages. PHP uses the -> as an object separator, in place of the "." in javascript. the usual string.toUpperCase() methods are now functions on their own(strtoupper, for example).As a pointer from now on, please, look up things on php.net before you do anything else. There is documentation for every function, and most include examples. It also contains the online manual, explaining the syntax and so on for PHP. I agree with you, Justsomeguy is taking a not very friendly tone, but I know where he comes from (... there was this one guy on another forum, didn't know what a variable was... i got banned.... :) ) being one of the helpers on this site, and others. Forgive him. We all lose our patience from time to time. It's only human.
Link to comment
Share on other sites

I'm not trying to talk down to anyone, I'm just trying to help. I'm also trying to quit smoking, so please excuse me if I'm not all puppies and flowers.If there is one thing I've learned throughout my time programming it is to trust error messages. What do you think this means:

Alright, so the message that is shown is as follows. "Unknown database 'dark_oblivion'" ...
Would you be seeing that message if there were a database called "dark_oblivion"? If the problem was something else, would you be seeing that message? I don't think so. I have to trust what you say though, I'm not sitting there watching over your shoulder so I don't know specifically what you're doing, but if you say you've set up the database then there's nothing else I have to offer. If you have a database set up and PHP is telling you that it can't find the database, one of you is confused.And, again, it always helps to make sure all error messages and notices are being displayed while in development or debugging.Thanks for the reply Jhecht, answering the same questions several times does wear on me after a while.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...