JP128 Posted June 22, 2006 Share Posted June 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/ Share on other sites More sharing options...
.josh Posted June 22, 2006 Share Posted June 22, 2006 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 hereif ($_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. Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48362 Share on other sites More sharing options...
JP128 Posted June 22, 2006 Author Share Posted June 22, 2006 parse error, unexpected "?>" in C:\FTB\jp128.mooo.com\htdocs\test.php Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48372 Share on other sites More sharing options...
.josh Posted June 22, 2006 Share Posted June 22, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48374 Share on other sites More sharing options...
JP128 Posted June 22, 2006 Author Share Posted June 22, 2006 I know. But I filled out the rest of the code.... See if you can find the error...<?phpmysql_connect("localhost", "***", "***", "***");//connect to database hereif ($_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"; }}?> Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48375 Share on other sites More sharing options...
shortj75 Posted June 22, 2006 Share Posted June 22, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48384 Share on other sites More sharing options...
JP128 Posted June 22, 2006 Author Share Posted June 22, 2006 Not working.. Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48389 Share on other sites More sharing options...
.josh Posted June 22, 2006 Share Posted June 22, 2006 post your entire code. Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48391 Share on other sites More sharing options...
JP128 Posted June 22, 2006 Author Share Posted June 22, 2006 <?phpmysql_connect("localhost", "user", "pass", "DB_name");//connect to database hereif ($_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"; }}?> Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48393 Share on other sites More sharing options...
.josh Posted June 22, 2006 Share Posted June 22, 2006 change this:mysql_connect("localhost", "user", "pass", "DB_name");to this:mysql_connect("localhost", "user", "pass");mysql_select_db("DB_name"); Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48397 Share on other sites More sharing options...
shortj75 Posted June 22, 2006 Share Posted June 22, 2006 this wont work to call your db[code]mysql_connect("localhost", "user", "pass", "DB_name");[/code]all mysql_connect will log you into 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] Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48398 Share on other sites More sharing options...
JP128 Posted June 22, 2006 Author Share Posted June 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48401 Share on other sites More sharing options...
shortj75 Posted June 22, 2006 Share Posted June 22, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48403 Share on other sites More sharing options...
.josh Posted June 22, 2006 Share Posted June 22, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12611-mysql-with-php/#findComment-48543 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.