Jump to content
xisto Community
Sign in to follow this  
lonebyrd

PHP: Need Help With MySQL Connection Code (Login Form)

Recommended Posts

I have a question about making a database. I recently downloaded a registration/login script and tried to use it, but then decided to go through a tutorial and make my own. Now, I made a config.php file which contains this:

<? php$username = "username";$password = "password";$database = "database";$server = "localhost";mysql_connect {$server, $username, $password} or die [mysql_error]}};mysql_connect_db{$database} or die [mysql error]}};?>

I made the database as written out in the tutorial, then typed in my website and it read:

'Parse error: parse error, unexpected T_VARIABLE in /home/lonebyrd/public_html/config.php on line 2'

My question is, is there somewhere that I have to enter my username, password, and email in the database so the registration page will come up?

Share this post


Link to post
Share on other sites

See that T_VARIABLE error is usually produced due to mismatched quotation marks.. single or double quotes - which i don't find in here.

 

But there's one thing you've to take note. The CONFIG file is to solely act as a configuration variable container. There shouldn't be any other code like the MySQL connection code you've included. This is a standard design model.

 

For example - your config.php should only look like:

<?php$username = "username";$password = "password";$database = "database";$server = "localhost";?>

And then in another file - which does the actual MySQL connection and data transactions, you have to INCLUDE the config file, so these variables become available... Say your file is login.php

<?phpmysql_connect ( $server, $username, $password ) or die ( mysql_error() );mysql_connect_db ( $database ) or die ( mysql error() );?>

BTW - you've got some weird stuff going on.. For example.. when you start your code block with the <?php statement, you've put a SPACE between the ? and php word. That might be the cause of your error.

 

Also in your mysql_connect and mysql_connect_db statements you've got a whole bunch of weird paranthesis going on - WHAT ARE THEY FOR ?? You're supposed to use ONLY the rounded paranthesis.. i.e. ( and ) ... !! How come the {} and [] ??

Share this post


Link to post
Share on other sites

Your code syntax seems to be completely wrong. In PHP arguments to functions are give in normal parenthesis (). For example: mysql_connect(params). And like M_E said, [mysql_error] is not a PHP statement. [ ] are used to indicate elements of an array (e.g. myArray[1] would point to second element of an array called myArray). And M_E is right about <? php causing errors. If short tags is enabled, <? is considered beginning of PHP code block, thus the 'php' would be parsed as a program code token, or actually it would just cause parse error as its not a valid token. If shottags is not enabled, then the block will not be considered as PHP at all and won't go through the parser at all.

Share this post


Link to post
Share on other sites

There is no PHP native function

mysql_connect_db()

there is one that you would use instead which is
mysql_select_db($database)
Also let it be noted

And like M_E said, [mysql_error]s not a PHP statement.

I did not take that from his post and there most certainly is a mysql_error() function read from the manual about that function PHP: mysql_error-Manual I use it all the time when first testing scripts I use it a little differently than they did but this is some of my code

$connect = mysql_connect($host,$user,$pass)  //Now connect to the server	or die("Could not connect to server".mysql_error()); $db =	mysql_select_db($Db,$connect)						or die("Could not select database $Db".mysql_error());
He just need to get his syntax correct, but hey we all learn from our mistakes. Also there shoul be no space between <? and php and it is a good practice to always use <?php instead of short tags like <? because not all servers have them turned on and when XML became in use it confuses the XML as well as PHP future version of PHP will come with short tags turned off as a default.

 

In the forums at PHP Builder where I answer question about failing scripts occasionally one of the most common errors is using short tags with a test server then the code does not work on the live web server, or they put the code on a different server and it doesn't work anymore. Another common problem is usage of checkboxes which to process with PHP should be written as an array by simply adding [] to the checkbox name.

 

Given more time lonebyrdwill learn from these errors and know in the future how to properly query the database. lonebyrd I would make a practice of using <?php to ensure that you code will work. A great online resource for learning PHP is a book called Practical PHP Programming

Share this post


Link to post
Share on other sites
<?phpmysql_connect("localhost", "username", "pass");mysql_select_db("database") or die(mysql_error());$sql = "SELECT * FROM  password WHERE ww = '" . $_POST['password']. "' AND nickname = '" . $_POST['nickname']. "' LIMIT 1"; $query = mysql_query($sql) or die(mysql_error());  if (mysql_num_rows($query) == 1){ $name = ucfirst($_POST[nickname]); $_SESSION['WNP'] = "$naam";echo "You are logged in"; echo $_SESSION['WNP']; echo "!<br>click <a href=\"index.php\">here</a> for admin."; }elseif (mysql_num_rows($query) == 0){ $text = "Not logged in!<br><a href=\"inloggen.php\">try</a> again!"; echo "$text";}}?>

Call the database for password this can also an txt or php extension

Calls every page with this code
<?phpinclude('variabel.php');	if($_SESSION['WNP'] == "") 	{ echo "Je bent niet ingelogd!<br>Klik <a href=\"inloggen.php\">hier</a> om in te loggen!";} 	elseif($_SESSION['WNP'] != "") 	{ Here the whole code }
this is it.

Its simple but handy for unauthorized people

Share this post


Link to post
Share on other sites

Houdini, in your response to my code, when you use $Db or any $database, am I supposed to type the name of my database or just leave it as it is. I'm just a little confused. I know I'm still learning and I'm sure I'll pick up more as I go along. I've already learned alot (albeit probably not as fast as most people around here).

Share this post


Link to post
Share on other sites

Alright lets take the code you have posted so far and make it understandable.

<? php // This must not have a space as it does currently$username = "username"; // You are assigning a literal name as a variable here$password = "password"; // You are assinging a literal password name to a variable here$database = "database";// You are assigning the name of a literal database name to a variable here$server = "localhost"; // You are assinging the literal name of the host to a variable hermysql_connect {$server, $username, $password} or die [mysql_error]}};//This is valid to connect to the server but the error handling is wrongmysql_connect_db{$database} or die [mysql error]}};// This is not it should be mysql_select_db($database) and same as above?>
Now lets fix the error
<?php $username = "username"; $password = "password"; $database = "database";$server = "localhost"; mysql_connect {$server, $username, $password} or die ("Could not connect to server".mysql_error());mysql_select_db{$database} or die ("Database select failed".mysql error());?>
Then lets actually do something with the selected database
$query  = "SELECT * FROM tablename";// The tablname is the table you want to query in the selected database.$result = mysql_query($query);//Execute the query
Usually the second part of the code merely assigns values to variables and then uses them to FIRST connect to the Database then to SECOND select a database to work with and then you send a query to the database concerning a specific table but what has been shown will do that and produce an error if encountered whilst conducting either the connection to the database server (mysql in this case) or selecting a database on that server.

Share this post


Link to post
Share on other sites

So, if I have this clear, my script would look like this:

<?php$nickname = "myCpanelname";$password = "myCpanelpassword";$database = "lonebyrd_FTV";$server = "localhost";mysql_connect {$server, $nickname, $password} or die ("Could not connect to server".mysql_error());mysql_select_db{$database} or die ("Database select failed".mysql error());$query  = "SELECT name; email; nickname; password; location; gender; date_of_birth; is_activated; activation_code; FROM registration";$result = mysql_query($query);?>

If I'm way off again, I might as well just give up. I'm reading the online book Practical PHP Programming, but that is taking some time (which is hard to get all in one shot)

Share this post


Link to post
Share on other sites

Yes that looks correct the SELECT * FROM tablename would just take all fields from the tablename whatever it is. After getting the result set or $result then you would display it or do something else with it. If you will read just the database section of book which is a great resource and always there.

Share this post


Link to post
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
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.