Jump to content


Photo

MySQL with PHP...


  • Please log in to reply
13 replies to this topic

#1 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 22 June 2006 - 04:08 AM

I need some help getting a script that connects to a database(I can get that). But then searches a table for a name and password.

SELECT * FROM table_name WHERE user="$username" AND pass="$password";

I have that part. But now I do not know how to integrate PHP in with it to see if the usrename and pass exists in there.
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 04:20 AM

if you have a form and you have 2 input elements one named username and one named password, and the user clicks submit, you would do for example:

//connect to database here

if ($_POST['username'] and $_POST['password']) {
   $username = $_POST['username'];
   $password = $_POST['password'];
   $sql = "select * from table_name where user = '$username' and pass = '$password'";
   $result = mysql_query($sql);
   $is_user = mysql_num_rows ($result);
   if ($is_user > 0) {
     echo "welcome $username";
   } else {
     echo "invalid username or password";
   }

that is an extremely simplified working example.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 22 June 2006 - 05:13 AM

parse error, unexpected "?>" in C:\FTB\jp128.mooo.com\htdocs\test.php
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 05:26 AM

well that code is obviously missing a few key ingredients, like php tags, db connection stuff, etc.. it was just an example of a piece of code to use inside the rest of your stuff...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 22 June 2006 - 05:31 AM

I know. But I filled out the rest of the code.... See if you can find the error...


<?php
mysql_connect("localhost", "***", "***", "***");
//connect to database here

if ($_POST['username'] and $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from table_name where user = '$username' and password = '$password'";
$result = mysql_query($sql);
$is_user = mysql_num_rows ($result);
if ($is_user > 0) {
echo "welcome $username";
} else {
echo "invalid username or password";
}
}
?>
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#6 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 22 June 2006 - 06:25 AM

everything looks fine to me except you have to change the table name in your sql query

 $sql = "select * from table_name where user = '$username' and password = '$password'";
                               

and you have to select a database in your mysql_connect

$conn=mysql_connect("host", "userid", "password");
if(!mysql_select_db("dbname",$conn))
     die("No database selected.");

other then that every thing looks fine
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#7 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 22 June 2006 - 06:55 AM

Not working..

-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 07:00 AM

post your entire code.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 22 June 2006 - 07:03 AM

<?php
mysql_connect("localhost", "user", "pass", "DB_name");
//connect to database here

if ($_POST['username'] and $_POST['password']) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "select * from login where user = '$username' and password = '$password'";
$result = mysql_query($sql);
$is_user = mysql_num_rows($result);
if ($is_user > 0) {
echo "welcome $username";
} else {
echo "invalid username or password";
}
}
?>
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 07:16 AM

change this:

mysql_connect("localhost", "user", "pass", "DB_name");

to this:

mysql_connect("localhost", "user", "pass");
mysql_select_db("DB_name");


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#11 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 22 June 2006 - 07:19 AM

this wont work to call your db

mysql_connect("localhost", "user", "pass", "DB_name");
all mysql_connect will log you in
to select a db you have to do something like this
$conn=mysql_connect("localhost", "userid", "password");
if(!mysql_select_db("dbname",$conn))
    die("No database selected.");


The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#12 JP128

JP128
  • Members
  • PipPipPip
  • Advanced Member
  • 233 posts
  • LocationWisconsin

Posted 22 June 2006 - 07:33 AM

I have connected to databases that way before.

OMG, I figured out what happened!

My script was fine.
I edited my HTML page with the form, saved it, but hit the back button. It didnt refresh the new HTML... That is why.


Sorry for wasting your time.
-----------------------------------------------------------------------
John Ensign - Age 16 - PHP Learner

My email is Johnny.Ensign@gmail.com

#13 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 22 June 2006 - 07:48 AM

its no problem thats what this place is for to help with problems but it is always better for the coder to figure out there own mistakes but if help is needed we are here
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#14 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 June 2006 - 05:46 PM

uh, i'm curious to know how exactly you managed to select the proper database using that 4th argument in mysql_connect... last time i checked, a 4th argument in mysql_connect is a boolean value to determine whether to establish a new link or not, when using the function again.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users