Connecting to MySql - Post ID 183833

User 2347322 Photo


Registered User
9 posts

hi

i hope i posted this thread at the right place :P

i am new to web development. i have no idea how does a website login to mysql database?

i have created a database at localhost, but no idea how to do it? search through google, try lots of sample but can't get it work.

My HTML scripts:
<form method="post" action="php/login.php" >
Login:<br />
<input type="text" name="login" /><br />
Password:<br />
<input type="text" name="pass" /><br />
<input type="submit" value="Sign in" /><br />
</form>

My PHP script:
<?
/*Use of Sessions*/
if(!session_id())
session_start();

header("Cache-control: private"); //avoid an IE6 bug (keep this line on top of the page)

$login='NO data sent';

/*simple checking of the data*/
if(isset($_POST['login']) && isset($_POST['pass']))
{

/*Connection to database logindb using your login name and password*/
$db=mysql_connect('localhost','userid','password') or die(mysql_error());
mysql_select_db('dentalpartner');

/*additional data checking and striping*/
$_POST['login']=mysql_real_escape_string(strip_tags(trim($_POST['login'])));
$_POST['pass']=mysql_real_escape_string(strip_tags(trim($_POST['pass'])));

$q=mysql_query("SELECT * FROM login WHERE user_id='{$_POST['login']}' AND login_pasword='{$_POST['pass']}'",$db) or die(mysql_error());

/*If there is a matching row*/
if(mysql_num_rows($q) > 0)
{
$_SESSION['login'] = $_POST['login'];
$login='Welcome back '.$_SESSION['login'];
}
else
{
$login= 'Wrong login or password';
}

mysql_close($db);

}

//you may echo the data anywhere in the file
echo $login;

?>


then my result come out:
The website encountered an error while retrieving http://localhost/php/login.php. It may be down for maintenance or configured incorrectly.



thanks in advance
jeff
User 187934 Photo


Senior Advisor
20,271 posts

PHP won't work on your local machine. Did you try it live online?:)
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2347322 Photo


Registered User
9 posts

oh, i didn't know that php not working on localhost eventhought i have apache running.

Is there anyway that i can test on the database? I did not try it online, still under initial development stage.

by the way, i can run phpinfo(); well on local machine?

thanks a lot
User 122279 Photo


Senior Advisor
14,648 posts
Online Now

PHP does run on a local machine if you have one of the wamp, winamp, xampp packages or so installed. I'm using it myself. Only, my PHP is not good enough to be of any help to you, Jeff.
Ha en riktig god dag!
Inger, Norway

My work in progress:
Components for Site Designer and the HTML Editor: https://mock-up.coffeecup.com


User 126492 Photo


Ambassador
1,525 posts

You have to have mysql installed as well on your local machine.

You can connect to a mysql database if you have xamp installed, though I have never tried it, I always login to my Cpanel.

Not sure if this would be any help to you:-

http://www.coffeecuphelp.com/form-data.php
Jim
---------------------------
User 891000 Photo


Ambassador
26 posts

Jeff,
I don't see anywhere in your script that you have connected to your database. Also, is the table in your database named "login"?
User 2347322 Photo


Registered User
9 posts

hi, i had finally connected to mysql at localhost. it seems like i did not activate mysql in php.ini, did some changes for php to communicate with mysql, apparently the default for all mysql was set a quote of ";" in front, i remove the ";" and add the add in -

include_path = ".;c:\php\includes"
extension_dir = "c:\php\extensions\"

then i set mysql.allow_persistent = On

and my php script similar to this:

$db_name="database"; // Database name
$tbl_name="table"; //table name

$dbhandle = mysql_connect('localhost', 'userid', 'password');
if ($dbhandle)
{
echo "Connected to MySQL<br>";

$userid=$_POST['userid'];
$userpass=$_POST['userpass'];

echo $userid.'<br/>';
echo $userpass.'<br/>';
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE userid='$userid' and password='$userpass'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count>=1){
echo "More than one result return";
}
else {
echo "Cannot connect table";
}

mysql_close($dbhandle);

} else {
echo "Unable to connect to MySQL<br>";
}


?>
User 2347322 Photo


Registered User
9 posts

guys, thanks for the usefull help

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.