[PHP] Connecting to a database
OK so here's my problem...I have a website that i built with HTML code and I have a form put in the website to login. I have very basic knowledge of how to code PHP, like I how to connect to a mysql database in the same file, but with even the littlest experience, anybody knows how to find the database information if its not in a seperate file. I made a seperate file to put the connection info into, but I am having trouble using that file to connect the login checking page to the database...if thats possible (like with the include function or something). Also, any help on how to make a register page (like how to actually put information into the database would be greatly appreciated. I have listed my form information (like the names and values and stuff) as a reference
LOGIN FORM CODE
Code:
Form Attributes:
<form name="login" method="post" action="checklog.php">
Username Box:
<input name="myusername" type="text" id="myusername">
Password Box:
<input name="mypassword" type="password" id="mypassword">
Submit Button:
ut type="submit" name="Submit" value="Login">
Here's what I've been using as a "connection file" to "include" in my other files (it's missing the start and end PHP tags and has been edited for database security):
Code:
$dbhost = 'HOST';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$conn = mysql_connect($dbhost,$dbuser,$dbpass)or die ('Error Connecting to Database. Please contact a site administrator');
$dbname = 'DATABASE';
mysql_select_db($dbname);
re: [PHP] Connecting to a database
You know... I'm giving this link out a little prematurely.. :/:
But I suppose it can't hurt ;)
I wrote an article about this [ame="http://community.zoovix.com/showthread.php?t=50"]here[/ame]. It should help you along. :thumbup1:
re: [PHP] Connecting to a database
PHP Code:
mysql_select_db($dbname, $conn);
maybe?
re: [PHP] Connecting to a database
PHP Code:
$conn = mysql_connect($host, $user, $password);
$selectdb = mysql_select_db('DATABASE', $conn);
if(!$selectdb)
{
echo "Database cannot be selected";}
else
{
echo "Database Connection has been established";}
or u can do
PHP Code:
$conn = mysql_connect($host, $user, $password);
mysql_select_db('DATABASE', $conn) or die ('Fatal Error');
maybe?
Re: [PHP] Connecting to a database
Fixed your title, it didn't have a language tag, had capitals on each word...made it readable again.
Re: [PHP] Connecting to a database
Pretty much what you would do is above.
I'm just going to post an example here for the heck of it
A simple mysql column data echo'er
Connection.php
PHP Code:
$host = "localhost";
$user = "root";
$pass = "root";
$db = "db";
$conn = mysql_connect($host, $user, $pass);//I believe its host, user, pass
$db = mysql_select_db($db, $conn);
index.php
PHP Code:
require_once('Connect.php');
$sql = "SELECT * FROM `test`";
$query = mysql_query($sql) or die ("Database error: ".mysql_error());
$row = mysql_fetch_array($query);
echo $row['column1'];