Hi all. I am completely new to PHP and MySQL and this forum, so I hope someone can help. I have just installed xampp (completely out of the box as it were) and am having problems with a simple script. The script is below

<?php

        ini_set('display_errors', 1);
        $connx = mysql_connect("localhost", "root");
       
        if($connx)
        {
	    echo ("connected to database");	
        }
        else
       {
            die("DB connection failed: " . mysql_error());
        } 
?>

I have also created a DB called 'test' with a table called 'members' containing
(int)id=1,
(varchar)username="jpc",
(varchar)password="1234"

My question is how do I

(1) specify the database name so that the database test is chosen?
(2) specify the table members?
(3) cycle through records (I know SQL and a loop will be needed)

I am trying to create a login script that checks the DB and table and finds a match.

I tried to use connect statement out of various tutorial such as
$connx = mysql_connect("localhost", "jpc", "1234"); I can see the logic behind this statement but this give me the following error/warning when parsed

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'jpc'@'localhost' (using password: YES) in C:\xampp\htdocs\checklogin.php on line 13
DB connection failed: Access denied for user 'jpc'@'localhost' (using password: YES)

ANY and I mean ANY enlightenment you guys can give me would be great.

P.S. Do I need to change anything in xampp (I am using xampp 1.7.2 if this helps and I haven't messed around with anything it is just as installed)

Many thanks for your time and effort in advance
JPC

Recommended Answers

All 14 Replies

Hi,
Here are simple procedure to make it happen
1. Connect to server

$host = "localhost";
$username = "root";
$password = "mypass";
$mydb = "testdb";
$connx = mysql_connect($host, $username, $password) or die("Cannot connect to server <br />".mysql_error());

2. select dabase (Like commandline USE)

mysql_select_db("$mydb") or die("Cannot select databaese $mydb <br />".mysql_error())

3. Query from whatever table you want from selected database

$query = "SELECT * FROM testTable ";
mysql_query($query) or die("Error:<br />".mysql_error())

Hope it helps :)

Wow, that was quick, will try it and let you know how I get on

many thanks for the reply

If you hit any wall, I will be happy to lend support!

Thanks so much, I now am able to connect to my database. I have 4 records and can find each one. I still need to tinker but I think I'm getting there. Wow, thanks for the help and this great forum

I have also included my script here for anyone to use

<?php
$host = "localhost";
$username = "root";
$password = "";
$mydb = "test";
$tbl_name="members"; // Table name
$connx = mysql_connect($host, $username, $password) or die("Cannot connect to server <br />".mysql_error());


if($connx)
{
echo("connected to database");
}


if(!$connx)
{
echo("no connection found");
}


mysql_select_db("$mydb") or die("Cannot select databaese $mydb <br />".mysql_error());


//  $query = "SELECT * FROM members ";
//      mysql_query($query) or die("Error:<br />".mysql_error());


// hard coded to test initially
//  $myusername = "jpc";
//  $mypassword = "1234";


// these are passed from a main_login script, 2 boxes for username and password...comment out is using the hard code above


$myusername=$_POST;
$mypassword=$_POST;


// To protect 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 = true;
exit;
}
else
{
echo "Wrong Username or Password";
}
?>

I hope it helps others

Thanks, can I ask one more question.....

OK I think I'm nearly there with the login script and I came across the following code. I can see the logic in this part but when I have the following code in the script I get the following error

if($count==1)
{
session_register($myusername);
session_register($mypassword);
header("location:login_success.php");
//$_SESSION = true;    // this was the original script
//exit;                                         // this was the original script
}

Obviously 'login_success.php' is the next part I have to tackle...back burner for now....

ERRORs/WARNINGs


Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\checklogin.php:10) in C:\xampp\htdocs\checklogin.php  on line 42


Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\checklogin.php:10) in C:\xampp\htdocs\checklogin.php on line 44

These only happen when I introduce the above code in script otherwise I get nothing but a message saying that I have connected to the database (which is what I expect)

Once again thanks for your time, energy and advice in advance
JPC

Your post is "simple...."
Make that simple one work and make it complex slowly

Your post is "simple...."
Make that simple one work and make it complex slowly

Hmmmm, I know what you mean, am trying to do that. Does this mean that the code above is not needed?

What exactly do you want to do? Once we know the answer then we'll be able to suggest/help

What exactly do you want to do? Once we know the answer then we'll be able to suggest/help

Hi thanks for the reply. I want to proceed to the next php script making sure that the user is logged in. I think that the 'session_register's' and the 'header' statement has something to do with it but I get those errors

As far as I know these are legal statements and can't tell why I get the errors

Any insights?

if($count==1)
{
session_register($myusername);
session_register($mypassword);
header("location:login_success.php");
//$_SESSION = true; // this was the original script
//exit; // this was the original script
}

Thanks again for your time in advance
JPC

want to tes if user is logged in right?
if yes then at very top of page add these line (before anything else)

session_start();
if($_SESSION['logged'] ){
    echo "You are logged in";
}
else{
//redirect to login page
header("Location:login.php");
}

and on your above code remember to remove exit after

$_SESSION['logged'] = true;

and add this

header("Location:members.php");

to redirect to members area.

Confused, just cry out and someone will answer ;)

and both members.php and login.php should be in one place for above code to work without modification. Use code tags to make your code readable

nice login script

Thanks for the code Jason! I've been trying to figure out how to do a login script as well and every time I've tried it, it's never recognized either the echo or if statements. I used your code and received a message, which is better than getting a message of nothing. Any help from anyone would be much appreciated.

Here is the message:
".mysql_error()); if($connx) { echo("connected to database"); } if(!$connx) { echo("no connection found"); } mysql_select_db("$mydb") or die("Cannot select databaese $mydb
".mysql_error()); // $query = "SELECT * FROM members "; // mysql_query($query) or die("Error:
".mysql_error()); // hard coded to test initially // $myusername = "jpc"; // $mypassword = "1234"; // these are passed from a main_login script, 2 boxes for username and password...comment out is using the hard code above $myusername=$_POST; $mypassword=$_POST; // To protect 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 = true; exit; } else { echo "Wrong Username or Password"; } ?>

would you repost the code, this time wrapping into code-tags?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.