Jump to content

Problems connecting to sql database


leeex

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/187882-problems-connecting-to-sql-database/
Share on other sites

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

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?

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

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.