my username Posted December 21, 2010 Share Posted December 21, 2010 Im trying to make a login box for my website and i have a problem here it is: I put all my info in the file. <?php $host = "localhost"; $user = "scape4le"; <--- thats my username $pass = "********";<--- my pass covered it out $db = "scape4le_members";<------ My DATABASE. ?> Is there any problem with the code? When i put it on my website i get this error. Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'DB_HOST' (1) in /home/scape4le/public_html/Login/login-exec.php on line 15 Failed to connect to server: Unknown MySQL server host 'DB_HOST' (1) Theres more codes for the whole login box but too much. But thats the file were the problems at. Here is the code that it says it has a error in Login-exec.php <?php //Start session session_start(); //Include database connection details require_once('config.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $login = clean($_POST['login']); $password = clean($_POST['password']); //Input Validations if($login == '') { $errmsg_arr[] = 'Login ID missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'Password missing'; $errflag = true; } //If there are input validations, redirect back to the login form if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: login-form.php"); exit(); } //Create query $qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'"; $result=mysql_query($qry); //Check whether the query was successful or not if($result) { if(mysql_num_rows($result) == 1) { //Login Successful session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_MEMBER_ID'] = $member['member_id']; $_SESSION['SESS_FIRST_NAME'] = $member['firstname']; $_SESSION['SESS_LAST_NAME'] = $member['lastname']; session_write_close(); header("location: member-index.php"); exit(); }else { //Login failed header("location: login-failed.php"); exit(); } }else { die("Query failed"); } ?> If anyone could help i would appreciate that, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/ Share on other sites More sharing options...
Pikachu2000 Posted December 21, 2010 Share Posted December 21, 2010 You're using variables to store the hostname, username, password and db name, but in your mysql_connect() and mysql_select_db(), you attempt to use constants which haven't been defined, thus PHP forces them to string values. With that in mind, your code should be more like this: $link = mysql_connect($host, $user, $pass); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } if( mysql_select_db($db) === FALSE ) { die("Unable to select database"); } Moving thread to PHP coding help, since this is ultimately a PHP issue. Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149756 Share on other sites More sharing options...
my username Posted December 21, 2010 Author Share Posted December 21, 2010 it says query failed You're using variables to store the hostname, username, password and db name, but in your mysql_connect() and mysql_select_db(), you attempt to use constants which haven't been defined, thus PHP forces them to string values. With that in mind, your code should be more like this: $link = mysql_connect($host, $user, $pass); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } if( mysql_select_db($db) === FALSE ) { die("Unable to select database"); } Moving thread to PHP coding help, since this is ultimately a PHP issue. Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149760 Share on other sites More sharing options...
Pikachu2000 Posted December 21, 2010 Share Posted December 21, 2010 Good. That means you've now connected to the database successfully. Change the query execution line to read: $result = mysql_query($qry) or die( "<br>Query string: $qry<br>Failed with error: " . mysql_error() ); Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149766 Share on other sites More sharing options...
my username Posted December 21, 2010 Author Share Posted December 21, 2010 Good. That means you've now connected to the database successfully. Change the query execution line to read: $result = mysql_query($qry) or die( "<br>Query string: $qry<br>Failed with error: " . mysql_error() ); Now it says Query string: SELECT * FROM members WHERE login='df' AND passwd='7885444af42e4b30c518c5be17c8850b' Failed with error: Table 'scape4le_members.members' doesn't exist Sorry im new at this. Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149768 Share on other sites More sharing options...
Pikachu2000 Posted December 21, 2010 Share Posted December 21, 2010 What the error is saying is that there is no table named `members` in the `scape4le_members` database. Double check to make sure the table actually exists in the database, and the spelling and capitalization of `members` is correct. Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149771 Share on other sites More sharing options...
my username Posted December 21, 2010 Author Share Posted December 21, 2010 What the error is saying is that there is no table named `members` in the `scape4le_members` database. Double check to make sure the table actually exists in the database, and the spelling and capitalization of `members` is correct. i made a table and now it says Query string: SELECT * FROM members WHERE login='yre' AND passwd='f614be5c658acd76f5c71592b6ec09e6' Failed with error: Unknown column 'passwd' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149774 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Share Posted December 21, 2010 do you mind if you screenshot your database? Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149919 Share on other sites More sharing options...
PFMaBiSmAd Posted December 21, 2010 Share Posted December 21, 2010 The error message is telling you what the problem is, Unknown column 'passwd' ... Do you have a column named passwd in your table? Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149923 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Share Posted December 21, 2010 The error message is telling you what the problem is, Unknown column 'passwd' ... Do you have a column named passwd in your table? maybe that will solve the prob Quote Link to comment https://forums.phpfreaks.com/topic/222267-mysql-having-errors-help-please/#findComment-1149926 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.