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

@  yordan : (16 June 2013 - 05:41 PM) You're Welcome, Agyat!
@  agyat : (16 June 2013 - 07:38 AM) Thanks Yordan...
@  velma : (16 June 2013 - 12:06 AM) I Have Asked Opa To Check For A Backup.. He'll Let Me Know Soon :)
@  velma : (16 June 2013 - 12:05 AM) T_T It Seems That Someone Has Deleted That Topic Since I Found The Url Of The Topic But It Gives Me An Error
@  yordan : (15 June 2013 - 10:31 PM) @velma : It's A Tuto On How To Create A Login Program.
@  yordan : (15 June 2013 - 10:31 PM) Happy Birthday To Youuuuuu Agyat!
@  yordan : (15 June 2013 - 10:31 PM) Ba$
@  agyat : (15 June 2013 - 04:41 PM) :(
@  agyat : (15 June 2013 - 04:41 PM) Where The Hall I Were? 15Th Is Almost At End And No-One Wished Me "happy Birthday"!!!
@  velma : (14 June 2013 - 10:39 AM) Which Tutorial Is He Searching For?
@  velma : (14 June 2013 - 10:38 AM) Which Tutorial Is He Searching For?
@  yordan : (14 June 2013 - 07:47 AM) Ok, Have A Look Tomorrow.
@  yordan : (13 June 2013 - 03:19 PM) @velma, Can You Have A Look At Feelay's Problem? Seems That His Tutorial Is Not Searchable Today.
@  Feelay : (13 June 2013 - 08:11 AM) Oh, Haha
@  velma : (12 June 2013 - 05:39 PM) T_T Lately My Levels Of Procrastination..... **sigh**
@  velma : (12 June 2013 - 05:38 PM) I'll Do It Later
@  velma : (12 June 2013 - 05:38 PM) Procrastinators.. People Who Keep Saying "i'll Do This In A Bit"
@  Feelay : (12 June 2013 - 02:05 PM) Deal Punishments To What?
@  velma : (12 June 2013 - 01:27 PM) T_T We Should Deal Punishments To Procrastinators... Especially Me
@  Feelay : (12 June 2013 - 12:06 PM) As Well As Making It More Secure.

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


Post Options

    • Can't make it out? Click here to generate a new image

  or Cancel


Topic Summary

Houdini

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.

lonebyrd

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)

Houdini

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.

lonebyrd

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

AVEX

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

Houdini

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

Hercco

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.

miCRoSCoPiC^eaRthLinG

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

lonebyrd

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?

Review the complete topic (launches new window)