Jump to content

[SOLVED] Problem connecting to MySQL


bnicholson.nc

Recommended Posts

Hi, I am having a problem connecting to my database using mysql_connect().

What I am trying to do is use an include file to store the values for host, user, password, and database. This is what I have so far...

 

(settings.inc)

<?php

 

$host="localhost";

$user="root";

$password="password";

$db="accountdb";   

 

?>

 

(create.php)

<?php

 

include("./settings.inc");

 

// Connects to your Database

mysql_connect($host, $user, $password);

mysql_select_db($db);

 

 

//This makes sure they did not leave any fields blank

if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] |

    !$_POST['email'] | !$_POST['expset']) {

die('You did not complete all of the required fields');

}

 

// checks if the username is in use

if (!get_magic_quotes_gpc()) {

$_POST['username'] = addslashes($_POST['username']);

}

$usercheck = $_POST['username'];

$check = mysql_query("SELECT username FROM account WHERE username = '$usercheck'")

or die(mysql_error());

$check2 = mysql_num_rows($check);

 

//if the name exists it gives an error

if ($check2 != 0) {

die('Sorry, the username '.$_POST['username'].' is already in use.');

}

 

// this makes sure both passwords entered match

if ($_POST['pass'] != $_POST['pass2']) {

die('Your passwords did not match. ');

}

 

// here we encrypt the password and add slashes if needed

$pw=strtoupper($_POST['pass']);

$un=strtoupper($_POST['username']);

$pass = sha1($un.":".$pw);

 

 

// now we insert it into the database

$insert = "INSERT INTO account (username, sha_pass_hash, email)

VALUES ('".$un."', '".$pass."', '".$_POST['email']."')";

$add_member = mysql_query($insert);

?>

 

 

<h1>Registered</h1>

<p>Thank you, you have registered - you may now login</a>.</p>

 

(registration.php)

<html>

<head>

<title>Registration Page</title>

</head>

<body bgcolor="#000000" alink="#FFFF00" vlink="#800080" link="#FF0000" text="#FFFFFF">

 

<form action="./create.php" method="post">

<table border="0">

<tr><td>Username:</td><td>

<input type="text" name="username" maxlength="60">

</td></tr>

<tr><td>Password:</td><td>

<input type="password" name="pass" maxlength="10">

</td></tr>

<tr><td>Confirm Password:</td><td>

<input type="password" name="pass2" maxlength="10">

</td></tr>

<tr><td>eMail:</td><td>

<input type="text" name="email" maxlength="25">

</td></tr>

<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr></table>

</form>

 

</body>

</html>

_____________________________________________________________

 

This code is no where near complete, but I can not figure out what the problem is.

When I enter the information into the fields and hit the Register button, I get a screen that tells me "No database selected"

 

Any help will be greatly appreciated.

 

 

 

 

 

Link to comment
Share on other sites

You should put some error checking in place so at least you can figure out where the problem is;

 

mysql_conenct()

 

The aboce link will take you to php.net and show you some good ways of connecting while error checking like this...

 

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

 

Also anyone will be able to access settings.inc. why not just call it settings.inc.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.