Jump to content

Archived

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

JP128

MySQL with PHP...

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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:

[code]
//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";
   }
[/code]

that is an extremely simplified working example.

Share this post


Link to post
Share on other sites
parse error, unexpected "?>" in C:\FTB\jp128.mooo.com\htdocs\test.php

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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";
}
}
?>

Share this post


Link to post
Share on other sites
everything looks fine to me except you have to change the table name in your sql query

[code]
$sql = "select * from table_name where user = '$username' and password = '$password'";
                              
[/code]

and you have to select a database in your mysql_connect

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

other then that every thing looks fine

Share this post


Link to post
Share on other sites
<?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";
}
}
?>

Share this post


Link to post
Share on other sites
change this:

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

to this:

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

Share this post


Link to post
Share on other sites
this wont work to call your db

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.