Jump to content

Connecting to database on a local server?


mondo

Recommended Posts

Im having a problem connection to my database on my local machine Here is the code i am using...

<?php mysql_connect("mysqlhost", "users", "users");@mysql_select_db(users) or die( "Unable to select database");$check = mysql_query("select username from users where username=\"$user\"");$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-userexists.php"); mysql_close($connection);  Die();}else{$check = mysql_query("select email from users where email=\"$email\"");$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-emailexists.php"); mysql_close($link); Die();}else $pass=md5($pass); $request = "INSERT INTO users values(NULL,\'$user\',\'$pass\', \'$email\',\'$zip\',)";$results = mysql_query($request);if($results) {header("Location: accountok.php"); }else  {header("Location: error-account.php");}mysql_close($link); Die();?>

I am using PHPmyadmin for my database. Im not sure if this is actually mysql .Im guessing "mysql_connect("mysqlhost", "users", "users");" mysqlhost = my IP addressusers = Database nameusers = TablesCan someone school me on this real quick?

Link to comment
Share on other sites

No, if you have the mysql installed on the same server as the script the you just use "localhost". Not "mysqlhost", however if you are using it on a different server then you would in fact use the ip for it (I think).

Link to comment
Share on other sites

You can use either the hostname or the IP as long as the server can reach it. If you want to use an IP, the IP of localhost is 127.0.0.1. If phpMyAdmin can connect to the MySQL server, then you should be able to as well. If you want to see what settings phpMyAdmin is using to connect, go to the phpMyAdmin folder and open up config.inc.php. Scroll down and you will see a section like this:

$cfg['Servers'][$i]['host']		  = 'localhost'; // MySQL hostname or IP address$cfg['Servers'][$i]['port']		  = '';		  // MySQL port - leave blank for default port$cfg['Servers'][$i]['socket']		= '';		  // Path to the socket - leave blank for default socket$cfg['Servers'][$i]['connect_type']  = 'tcp';	   // How to connect to MySQL server ('tcp' or 'socket')$cfg['Servers'][$i]['compress']	  = FALSE;	   // Use compressed protocol for the MySQL connection													// (requires PHP >= 4.3.0)$cfg['Servers'][$i]['controluser']   = '';		  // MySQL control user settings													// (this user must have read-only$cfg['Servers'][$i]['controlpass']   = '';		  // access to the "mysql/user"													// and "mysql/db" tables)$cfg['Servers'][$i]['auth_type']	 = 'config';	// Authentication method (config, http or cookie based)?$cfg['Servers'][$i]['user']		  = 'root';	  // MySQL user$cfg['Servers'][$i]['password']	  = '';		  // MySQL password (only needed

That info gives you the host, user name, and password that you use with mysql_connect.

Link to comment
Share on other sites

the usual connection for local database is:

mysql_connect("localhost", "root"); // password is not needed for local connection (in general)

than you need to select your database:

mysql_select_db users or die( "Unable to select database"); //quotations arent needed for the name of the database selected.

Link to comment
Share on other sites

@thibo1025I don't think this would work

mysql_select_db users or die( "Unable to select database");

mysql_select_db is a function so parameters are passed in ()>>

mysql_select_db("users") or die( "Unable to select database");

Link to comment
Share on other sites

Thanks guys, still having problems like usaul! This is the error message i get Line 79 = header("Location: error-account.php"); ( I don't see what the problem is, perhaps ive yet to create an error-account file?)Line 83 = mysql_close($link);Heres the entire code. Might get a btter understanding.

<?php mysql_connect("localhost", "root", "");mysql_select_db(users) or die( "Unable to select database");$check = mysql_query("select username from users where username=\"$user\"");$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-userexists.php"); mysql_close($connection); Die();}else{$check = mysql_query("select email from users where email=\"$email\"");$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-emailexists.php"); mysql_close($link); Die();}else $pass=md5($pass); $request = "INSERT INTO users values(NULL,\'$user\',\'$pass\', \'$email\',\'$zip\',)";$results = mysql_query($request);if($results) {header("Location: accountok.php"); }else  {header("Location: error-account.php");}mysql_close($link); Die();?>

Link to comment
Share on other sites

tested and works :)

<?php$link = mysql_connect("localhost", "root", "qwebqwe");@mysql_select_db("users") or die( "Unable to select database");$sql="SELECT username FROM users WHERE username='".$user."'";$check = mysql_query($sql);$returned = mysql_fetch_array($check);if(!empty($returned)){	header("Location: error-userexists.php");	mysql_close($link); 	Die();}$sql2="SELECT email FROM users WHERE email='".$email."'";$check = mysql_query($sql2);$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-emailexists.php");mysql_close($link);Die();}$pass=md5($pass);$request = "INSERT INTO users values(NULL,'".$user."','".$pass."', '".$email."','".$zip."',)";$results = mysql_query($request);if($results){header("Location: accountok.php");}else {header("Location: error-account.php");}mysql_close($link);?>

I couldn't understand something here, how could you get errors on lines 34 , 79 and 83 while the script has only 44 lines :S

Link to comment
Share on other sites

Man! Just when i thought this would work. I'm getting the same error again "Cannot modify header information - headers already sent " that is on this line....header("Location: error-account.php");What does it mean it has already been sent? Also, i noticed that you added a password for the sql connect. I was gettin an error for that until i took the pass out. Oh yea, the reason there was only 44 lines was because i erased all of the "notes" before posting the code.Anyone know whats goin on with that error? Should i just take that line out?Heres the full code again, i hope this is not annoying you guys as much as it is for me.

<?php    $url = 'http://www.-------.com';    $user = $_POST['username'];    $pass = $_POST['password'];    $pass2 = $_POST['password2'];    $zip = $_POST['zip'];    $email = $_POST['email'];    $min_lenngth = 6;    function CheckMail($email){        if(eregi("@", $email)){            return true;        }else {            return false;        }    }    function CheckZip($zip) {        if (eregi("[0-9]", $zip)){            return true;        }else{            return false;        }    }    if (($pass)!=($pass2)){        echo "Passwords to not match!";        exit;    }elseif((empty($email)) || (!CheckMail($email))){        echo "E-mail is not valid";        exit;    }elseif(strlen($user) < $min_lenngth || strlen($pass) < $min_lenngth){        echo "Password and or username is too short";        exit;    }else{        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';    }?><?php$link = mysql_connect("localhost", "root", "");@mysql_select_db("users") or die( "Unable to select database");$sql="SELECT username FROM users WHERE username='".$user."'";$check = mysql_query($sql);$returned = mysql_fetch_array($check);if(!empty($returned)){    header("Location: error-userexists.php");    mysql_close($link);    Die();}$sql2="SELECT email FROM users WHERE email='".$email."'";$check = mysql_query($sql2);$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-emailexists.php");mysql_close($link);Die();}$pass=md5($pass);$request = "INSERT INTO users values(NULL,'".$user."','".$pass."', '".$email."','".$zip."',)";$results = mysql_query($request);if($results){header("Location: accountok.php");}else {header("Location: error-account.php");}mysql_close($link);?>

Link to comment
Share on other sites

The Headers already sent error comes from the php coding, not the SQL.There are several ways to get rid of the error. Essentially, you need to confirm that NOT ONE SINGLE thing gets sent to the Browser before the Header. Not even a single blank / empty character or line feed or anything. Have a look at the php Forum for any further explanation. There have been several discussions lately about this. Since these echo statements are (potentially) being issued before the re-direct, they are probably to blame.

if (($pass)!=($pass2)){echo "Passwords to not match!";

Link to comment
Share on other sites

Also, i noticed that you added a password for the sql connect. I was gettin an error for that until i took the pass out.
Oops I changed the password to test in my computer and I forgot it.I just removed this line
<?php$url = 'http://www.-------.com';$user = $_POST['username'];$pass = $_POST['password'];$pass2 = $_POST['password2'];$zip = $_POST['zip'];$email = $_POST['email'];$min_lenngth = 6;function CheckMail($email){if(eregi("@", $email)){return true;}else {return false;}}function CheckZip($zip) {if (eregi("[0-9]", $zip)){return true;}else{return false;}}if (($pass)!=($pass2)){echo "Passwords to not match!";exit(1);}elseif((empty($email)) || (!CheckMail($email))){echo "E-mail is not valid";exit(1);}elseif(strlen($user) < $min_lenngth || strlen($pass) < $min_lenngth){echo "Password and or username is too short";exit(1);}$link = mysql_connect("localhost", "root", "");@mysql_select_db("users") or die( "Unable to select database");$sql="SELECT username FROM users WHERE username='".$user."'";$check = mysql_query($sql);$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-userexists.php");mysql_close($link);exit(1);}$sql2="SELECT email FROM users WHERE email='".$email."'";$check = mysql_query($sql2);$returned = mysql_fetch_array($check);if(!empty($returned)){header("Location: error-emailexists.php");mysql_close($link);exit(1);}$pass=md5($pass);$request = "INSERT INTO users values(NULL,'".$user."','".$pass."', '".$email."','".$zip."')";$results = mysql_query($request);if($results){header("Location: accountok.php");}else {header("Location: error-account.php");}mysql_close($link);?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...