leeex Posted January 9, 2010 Share Posted January 9, 2010 Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\checklogin.php on line 9 cannot connect My code for checklogin.php is : <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="junior"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$localhost", "$root", "$junior")or die("cannot connect"); mysql_select_db("$test")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> any ideas? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/ Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 Your using variables that don't exist. mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); Also, wrapping variables within quotes for no good reason is never a good idea. Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992051 Share on other sites More sharing options...
leeex Posted January 10, 2010 Author Share Posted January 10, 2010 Well I got the code from : http://www.phpeasystep.com/workshopview.php?id=6 I took your advise & revised the checklogin.php page: <?php include("layout.php"); ?> <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="junior"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> <?php include("layout2.php"); ?> But now I'm getting this message: WARNING: session_register() [function.session-register]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\layout.php:144) in C:\XAMPP\HTDOCS\CHECKLOGIN.PHP on line 30 WARNING: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\layout.php:144) in C:\XAMPP\HTDOCS\CHECKLOGIN.PHP on line 30 WARNING: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\layout.php:144) in C:\XAMPP\HTDOCS\CHECKLOGIN.PHP on line 32 Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992053 Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 session_register has long been deprecated and should no longer be used. Just set a value within the $_SESSION array instead. You also need to place session_start within your script, somewhere before ANY output is sent to the client. Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992056 Share on other sites More sharing options...
leeex Posted January 10, 2010 Author Share Posted January 10, 2010 I put in the session_start(); : <?php include("layout.php"); ?> <?php session_start(); $host="localhost"; // Host name $username="root"; // Mysql username $password="junior"; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> <?php include("layout2.php"); ?> But I'm still new to PHP. Where's the $_SESSION array and what value do I set it to? Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992066 Share on other sites More sharing options...
leeex Posted January 10, 2010 Author Share Posted January 10, 2010 With the previous code mentioned, I'm getting this error now: WARNING: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\layout.php:144) in C:\XAMPP\HTDOCS\CHECKLOGIN.PHP on line 3 WARNING: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\layout.php:144) in C:\XAMPP\HTDOCS\CHECKLOGIN.PHP on line 3 WARNING: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\layout.php:144) in C:\XAMPP\HTDOCS\CHECKLOGIN.PHP on line 33 Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992067 Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 As I said, session_start needs to go BEFORE any output. I assume layout.php sends output, you don't need it. As for the use of the $_SESSION array. Replace this..... if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } with.... if($count==1){ $_SESSION['logged'] = true; header("location:login_success.php"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992072 Share on other sites More sharing options...
leeex Posted January 10, 2010 Author Share Posted January 10, 2010 wow, it works now! thankss!(: Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992077 Share on other sites More sharing options...
trq Posted January 10, 2010 Share Posted January 10, 2010 You need to find yourself a more up to date resource for learning php. There's a free book in my signature (hudzilla) that is pretty comprehensive and up to date. Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-992081 Share on other sites More sharing options...
fenway Posted January 16, 2010 Share Posted January 16, 2010 And next time, post in the php board. Quote Link to comment https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/#findComment-995943 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.