john.muckley Posted April 7, 2013 Share Posted April 7, 2013 (edited) Hey guys, Quick one for you. I'm trying to use include for my db connection variables, but for some unknown reason, it wont have any of it. so my file 'usersmysql.php' contains this... $host="127.0.0.1"; $username="globaluser"; $password="d4TXz4ATHgcvqJx"; $db_name="project1"; $tbl_name="useraccounts"; and my main script is this... <?php include("usersmysql.php"); mysql_connect("$host", "$username", "$password")or die("There was a problem connecting to the database"); mysql_select_db("$db_name")or die("cannot select DB"); $myusername=$_POST['loginusr']; $myplainpassword=$_POST['loginpwd']; $mypassword=md5($myplainpassword); $myusername = stripslashes($myusername); $myusername = mysql_real_escape_string($myusername); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:userhome.php"); } else { //$interact = 'Invalid username and/or password'; $_SESSION['interact'] = $count; header("location:login.php?response=1"); } ?> But when i test this, all i get is a dead output... $host="127.0.0.1"; // Host name $username="globaluser; // Mysql username $password="d4TXz4ATHgcvqJx"; // Mysql password $db_name="project1"; // Database name $tbl_name="useraccounts"; // Table name There was a problem connecting to the database The variables listed in the output are correct, so it obviously reads the include file, but i cant see why its not actioning it correclty. Any help appriciated!! Edited April 7, 2013 by john.muckley Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/ Share on other sites More sharing options...
PaulRyan Posted April 7, 2013 Share Posted April 7, 2013 You missed the closing quote on this line. $username="globaluser; Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/#findComment-1423444 Share on other sites More sharing options...
john.muckley Posted April 7, 2013 Author Share Posted April 7, 2013 (edited) ahhh so i did! infact, i edited the username and password as i was typing the post to be safe and deleted an extra character. it is there in the actual 'usersmysql.php' file. I should also add that when i copy and paste the exact contents of the usersmysql.php into the main script and comment out the include, it works straight away. Edited April 7, 2013 by john.muckley Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/#findComment-1423445 Share on other sites More sharing options...
Solution PaulRyan Posted April 7, 2013 Solution Share Posted April 7, 2013 In the "usersmysql.php" do you have the opening PHP tags? Like so: <?PHP $host="127.0.0.1"; $username="globaluser"; $password="d4TXz4ATHgcvqJx"; $db_name="project1"; $tbl_name="useraccounts"; Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/#findComment-1423446 Share on other sites More sharing options...
john.muckley Posted April 7, 2013 Author Share Posted April 7, 2013 ahhh i have solved my problem! I didnt realise i had to include <?php and ?> on the include file, so that was missing! added in, and worked straight away!! Thanks guys!!! Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/#findComment-1423447 Share on other sites More sharing options...
john.muckley Posted April 7, 2013 Author Share Posted April 7, 2013 In the "usersmysql.php" do you have the opening PHP tags? Like so: <?PHP $host="127.0.0.1"; $username="globaluser"; $password="d4TXz4ATHgcvqJx"; $db_name="project1"; $tbl_name="useraccounts"; That was exactly the problem!!!! Thanks for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/#findComment-1423448 Share on other sites More sharing options...
PaulRyan Posted April 7, 2013 Share Posted April 7, 2013 You don't need the end tags, in fact it is considered bad practice to have them when including files. It prevents the dread header already sent error that happens when you leave space after the closing tag. It is best to emit them. Quote Link to comment https://forums.phpfreaks.com/topic/276664-include-what-am-i-doing-wrong/#findComment-1423450 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.