Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  agyat : (23 May 2013 - 01:23 AM) Wow! Mr. Sb Back Home.
@  OpaQue : (23 May 2013 - 12:44 AM) Ting
@  OpaQue : (24 April 2013 - 02:44 PM) I guess, Time to run Mycent script.
@  OpaQue : (24 April 2013 - 02:43 PM) wow.. not much spam. except habatt posting lot of links.. :P
@  yordan : (23 April 2013 - 01:04 PM) You're welcome, agyat. Nice to have been helpful. Second lesson: try full words, "you" instead of "EW".
@  agyat : (23 April 2013 - 05:03 AM) @YORDAN: tHANK EW FOR YOUR FIRST LESSON.   :D
@  yordan : (22 April 2013 - 09:43 PM) @agyat : "why don't you help me", or "please help me", or "please teach us"
@  yordan : (22 April 2013 - 09:42 PM) welcome back, velma
@  velma : (22 April 2013 - 07:51 AM) **yawns** Good to be back, wonder what is going on here :)
@  agyat : (22 April 2013 - 03:50 AM) Oh! so, why don't help me learn english..
@  yordan : (21 April 2013 - 08:38 PM) The goal mentioned by shiu : "learning english, learning computer"
@  agyat : (21 April 2013 - 06:31 PM) WHAT GOAL?
@  yordan : (20 April 2013 - 10:39 AM) yes, that's our goal. simultaneouly learning English and teaching/learning computer using.
@  shiyu : (20 April 2013 - 07:30 AM) learning english,learning computer
@  yordan : (19 April 2013 - 01:11 PM) Oh, I see, it's just a trick in order to force people looking at your texte. Somehow smart, maybe.
@  agyat : (19 April 2013 - 02:54 AM) And of course I know it is not SEO friendly.
@  agyat : (19 April 2013 - 02:52 AM) There may be two possible answers for that ....


1) Shout was posted using mobile keypad.

2) To force people read content carefully and/or with more concentration.
@  agyat : (19 April 2013 - 02:49 AM) There may be two possible answers for that ....
@  yordan : (18 April 2013 - 09:35 PM) however, why this mixing of capital letters in the middle of your text?
@  agyat : (18 April 2013 - 11:10 AM) false feelings.

Photo
- - - - -

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


8 replies to this topic

#1 lonebyrd

lonebyrd

    Premium Member

  • Members
  • 302 posts
  • Gender:Female
  • Location:Northeastern Connecticut USA
  • myCENTs:38.43

Posted 26 April 2006 - 04:26 AM

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?

#2 miCRoSCoPiC^eaRthLinG

miCRoSCoPiC^eaRthLinG

    PsYcheDeLiC dR3aMeR

  • [MODERATOR]
  • 2,248 posts
  • Gender:Male
  • Location:Bangkok, Thailand
  • Interests:Photography, Magic Tricks, Numismatics & Philately to some extent, Being a nuisance in general (that's my favourite)
  • myCENTs:NEGATIVE[-21.50]

Posted 26 April 2006 - 05:22 AM

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
<?php

mysql_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 [] ??

#3 Hercco

Hercco

    Super Member

  • Members
  • 595 posts

Posted 01 May 2006 - 12:15 PM

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.

#4 Houdini

Houdini

    Super Member

  • Members
  • 572 posts
  • Location:Nashville Tennessee

Posted 01 May 2006 - 01:09 PM

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

#5 AVEX

AVEX

    Newbie [ Level 1 ]

  • Members
  • 7 posts

Posted 07 May 2006 - 06:26 AM

<?php

mysql_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
<?php
include('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

#6 lonebyrd

lonebyrd

    Premium Member

  • Members
  • 302 posts
  • Gender:Female
  • Location:Northeastern Connecticut USA
  • myCENTs:38.43

Posted 07 May 2006 - 04:45 PM

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).

#7 Houdini

Houdini

    Super Member

  • Members
  • 572 posts
  • Location:Nashville Tennessee

Posted 07 May 2006 - 05:22 PM

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 her

mysql_connect {$server, $username, $password} or die [mysql_error]}};//This is valid to connect to the server but the error handling is wrong
mysql_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.

#8 lonebyrd

lonebyrd

    Premium Member

  • Members
  • 302 posts
  • Gender:Female
  • Location:Northeastern Connecticut USA
  • myCENTs:38.43

Posted 07 May 2006 - 09:46 PM

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)

#9 Houdini

Houdini

    Super Member

  • Members
  • 572 posts
  • Location:Nashville Tennessee

Posted 07 May 2006 - 10:14 PM

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.



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users