rmmo Posted July 1, 2008 Share Posted July 1, 2008 Hi all, well this is my first post in here i have had a read of the rules and think i get them all... i just hope im writing this in the right place. on to my problem. i have a mysql database containing information about customers that will login to my site... now without being too complicated i want to vairify if the username and password passed from my form are present in the database.. so i have used action = POST to send the values from the form and $_POST to get assign the values to variables $username and $password... i can echo them without issue. i can also carry out a query and echo the rows from the table... now what i want to do is to compare the results from the sql query and the values in $username and $password... i had a go and this below is what i came up with... ..... if($connect_mysql) { echo "connection established to MYSQL"; } else { die ("Connection could not be established with MYSQL"); } $mysql_db = mysql_select_db("artsonlinermmo"); if($mysql_db) { echo "database mounted"; } else { die("could not mount AORMMO database"); } $query = ("SELECT customer_name, customer_password FROM customer where customer_name like $username"); $results = mysql_query($query); while($row = mysql_fetch_assoc($results)) if($username = $row['customer_name']); { echo "great your a user!"; } else //(!$username == $row['customer_name']); { die("sorry not a user"); } ?> ..... now when i browse the form and submit it... i get the following error message... i gather it is about the ELSE i have up there but i have gone over it a hundred times and cant see any problem with it... any help would be greatly appreciated! thanks in advanced RMMO PS this is only day 2 of using PHP for me so im trying to keep it simple... (this is supposed to be a login page) Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/ Share on other sites More sharing options...
GreenUser Posted July 1, 2008 Share Posted July 1, 2008 Hi and welcome, let's see if this helps If it is this line you are referring to, else //(!$username == $row['customer_name']); I would change it to else (!$username == $row['customer_name']); Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579627 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 else can't have any criteria associated with it. It can't have a situation in which it does things, rather. Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579636 Share on other sites More sharing options...
kenrbnsn Posted July 1, 2008 Share Posted July 1, 2008 One problems is that you're using a single "=" in this "if" <?php if($username = $row['customer_name']); ?> Single "=" are for assignment. Use double equal "==" for comparison: <?php if($username == $row['customer_name']); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579639 Share on other sites More sharing options...
rmmo Posted July 1, 2008 Author Share Posted July 1, 2008 im sorry about the comment... but that wasnt the trouble... i know that an else cant have any conditions attached. thats why the comment was there..... previously i tried to use a ELSEIF.. that didnt work either... the == i didnt know about but sadly that hasnt solved it,.... the error is: Parse error: syntax error, unexpected T_ELSE in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\artsonlinestorermmologin.php on line 52 thats the line with the ELSE on it... but you could guess that from the error thanks for all the help! still not solved any more help would be greatly appreciated RMMO Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579644 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Oh. You missed a } before the else I think. Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579646 Share on other sites More sharing options...
jelly Posted July 1, 2008 Share Posted July 1, 2008 <?php // $username = mysql_real_escape_string($_POST['username']); if($connect_mysql) { echo 'connection established to MYSQL'; } else { die ('Connection could not be established with MYSQL'); } $mysql_db = mysql_select_db('artsonlinermmo'); if($mysql_db) { echo 'database mounted'; } else { die('could not mount AORMMO database'); } $query = ("SELECT `customer_name`, `customer_password` FROM `customer` WHERE `customer_name` = '$username'"); $results = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($results)) { if($username == $row['customer_name']) { $loggedin = true; echo 'great your a user!'; } else { $loggedin = false; echo 'sorry not a user'; exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579649 Share on other sites More sharing options...
.josh Posted July 2, 2008 Share Posted July 2, 2008 <?php session_start(); $host = 'localhost'; // or your db host $dbuser = 'xxxxx'; // db username $dbpass = 'xxxxx'; // db password $dbname = 'xxxxx'; // db name // connect to and select db $conn = mysql_connect($host, $dbuser, $dbpass) or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db($dbname, $conn) or trigger_error("SQL", E_USER_ERROR); // if there are posted vars... if ($_POST['username'] && $_POST['password']) { // sanitize $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); // see if they are in table $query = "SELECT `customer_name`, `customer_password` FROM `customer` WHERE `customer_name` = '$username' AND `customer_password` = '$password'"; $results = mysql_query($query, $conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows returned. $rows = mysql_num_rows($results); // if something returned... if ($rows > 0) { // user/pass exist, do something, like make a session var signifying user is logged in $_SESSION['loggedin'] = true; // if nothing returned... } else { //user/pass do not exist, do something } // end if..else $rows // if no username and/or pass posted... } else { // username and/or password not entered in form, do something } // end if..else username/password ?> Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579691 Share on other sites More sharing options...
rmmo Posted July 2, 2008 Author Share Posted July 2, 2008 Wow thanks alot for all that help everyone the last post by Crayon Violent is the one i went with... it worked great! no i know this is probably really bad but it threw an error for the session_start() saying that the headers were allready sent.. so i commented that bit out.... will i go to PHP hell for that? anyway im going to say that this is <SOLVED> thanks again to all that helped! much appreciated! RMMO Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-579944 Share on other sites More sharing options...
.josh Posted July 2, 2008 Share Posted July 2, 2008 That means you had output before the session_start();. Is this being included in some other file that already has output? Do you have any whitespace or lines before your opening <?php tag if you don't? Without session_start(); you cannot make that session variable 'loggedin' so if you want to make use of session vars for data persistence from page to page, you need session_start(); at the top of every page that will use it. If that script is being included in some other page, put it at the top of the page that's including it. Quote Link to comment https://forums.phpfreaks.com/topic/112850-solved-how-to-reference-data-from-a-mysql-query-with-php/#findComment-580131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.