Forum Moderators: coopster
mysqlconnect.php code:
<?php # Script 16.4 - mysqli_connect.php
// This file contains the database access information.
// This file also establishes a connection to MySQL
// and selects the database.
// Set the database access information as constants:
DEFINE ('DB_USER', 'UserName');
DEFINE ('DB_PASSWORD', 'Password');
DEFINE ('DB_HOST', 'Localhost');
DEFINE ('DB_NAME', 'Databases');
// Make the connection:
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
trigger_error ('Could not connect to MySQL: ' . mysql_connect_error() );
}
?>
Then here is my config file
config.inc file code:
[codes]
<?php # Script 16.3 - config.inc.php
/* This script:
* - define constants and settings
* - dictates how errors are handled
* - defines useful functions
*/
// Document who created this site, when, why, etc.
// ********************************** //
// ************ SETTINGS ************ //
// Flag variable for site status:
define('LIVE', True);
// Admin contact address:
define('EMAIL', 'salbertin@bspromo.cc');
// Site URL (base for all redirections. This is the address they will be redirected to if they try to access a protected page and they are not logged in.):
define ('BASE_URL', 'http://www.bspromo.cc/login/');
// Location of the MySQL connection script:
define ('MYSQL', 'mysqli_connect.php');
// Adjust the time zone for PHP 5.1 and greater:
// ************ SETTINGS ************ //
// ********************************** //
// ****************************************** //
// ************ ERROR MANAGEMENT ************ //
// Create the error handler:
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
// Build the error message.
$message = "<p>An error occurred in script '$e_file' on line $e_line: $e_message\n<br />";
// Add the date and time:
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
// Append $e_vars to the $message:
$message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n</p>";
if (!LIVE) { // Development (print the error).
echo '<div class="error">' . $message . '</div><br />';
} else { // Don't show the error:
// Send an email to the admin:
mail(EMAIL, 'Site Error!', $message, 'From: salbertin@bspromo.cc');
// Only print an error message if the error isn't a notice:
if ($e_number != E_NOTICE) {
echo '<div class="error">A system error occurred. We apologize for the inconvenience.</div><br />';
}
} // End of !LIVE IF.
} // End of my_error_handler() definition.
// Use my error handler.
set_error_handler ('my_error_handler');
// ************ ERROR MANAGEMENT ************ //
// ****************************************** //
?>
[/codes]
This is the register php code:
[code]
<?php
# Script 16.6 - register.php
// This is the registration page for the site.
require_once ('includes/config.inc.php');
$page_title = 'Register';
include ('includes/header.html');
if (isset($_POST['submitted'])) { // Handle the form.
require_once (MYSQL);
// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);
// Assume invalid values:
$fn = $ln = $e = $p = FALSE;
// Ln 19 Check for a first name:
if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['first_name'])) {
$fn = mysql_real_escape_string ($trimmed['first_name']);
} else {
echo '<p class="error">Please enter your first name!</p>';
}
// ln 26 Check for a last name:
if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['last_name'])) {
$ln = mysql_real_escape_string ($trimmed['last_name']);
} else {
echo '<p class="error">Please enter your last name!</p>';
}
// Ln 33 Check for an email address:
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) {
$e = mysql_real_escape_string ($trimmed['email']);
} else {
echo '<p class="error">Please enter a valid email address!</p>';
}
// Ln 41 Check for a password and match against the confirmed password:
if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) {
if ($trimmed['password1'] == $trimmed['password2']) {
$p = mysql_real_escape_string ($trimmed['password1']);
} else {
echo '<p class="error">Your password did not match the confirmed password!</p>';
}
} else {
echo '<p class="error">Please enter a valid password!</p>';
}
if ($fn && $ln && $e && $p) { // If everything's OK...
// Line 54 Make sure the email address is available:
$q = "SELECT user_id FROM users WHERE email='$e'";
$r = mysql_query($q, $dbc) or trigger_error("Query: $q\n<br />MySQL Error: " . mysql_error($dbc));
if (mysql_num_rows($r) == 0) { // Available.
// Ln 60 Create the activation code:
$a = md5(uniqid(rand(), true));
// Ln 63 Add the user to the database:
$q = "INSERT INTO users (email, pass, first_name, last_name, active, registration_date) VALUES ('$e', SHA1('$p'), '$fn', '$ln', '$a', NOW() )";
$r = mysql_query ($q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysql_error($dbc));
if (mysql_affected_rows($dbc) == 1) { // If it ran OK.
// Ln 69 Send the email:
$body = "Thank you for registering at <Work it Local!>. To activate your account, please click on this link:\n\n";
$body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a";
mail($trimmed['email'], 'Registration Confirmation', $body, 'From: salbertin@bspromo.cc');
// Ln 74 Finish the page:
echo '<h3>Thank you for registering! A confirmation email has been sent to your address. Please click on the link in that email in order to activate your account.</h3>';
include ('includes/footer.html'); // Include the HTML footer.
exit(); // Ln 77 Stop the page.
} else { // Ln 79 If it did not run OK.
echo '<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
}
} else { // Ln 83 The email address is not available.
echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>';
}
} else { // Ln 87 If one of the data tests failed.
echo '<p class="error">Please re-enter your passwords and try again.</p>';
}
mysql_close($dbc);
} // Ln 93 End of the main Submit conditional.
?>
<h1>Register</h1>
<form action="register.php" method="post">
<fieldset>
<p><b>First Name:</b> <input type="text" name="first_name" size="20" maxlength="20" value="<?php if (isset($trimmed['first_name'])) echo $trimmed['first_name']; ?>" /></p>
<p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="40" value="<?php if (isset($trimmed['last_name'])) echo $trimmed['last_name']; ?>" /></p>
<p><b>Email Address:</b>
<input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed['email']; ?>" /></p>
<p><b>Password:</b>
<input type="password" name="password1" size="20" maxlength="20" />
<small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small></p>
<p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Register" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php // Include the HTML footer.
include ('includes/footer.html'); ?>
These are the errors I am receiving:
MySQL Error: No database selected
<p>An error occurred in script '/homepages/1/d125499918/htdocs/BrainStick/login/register.php' on line 58: mysql_num_rows(): supplied argument is not a valid MySQL result resource
MySQL Error: No database selected
Any help or insight is greatly appreciated
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
and put this lines at top of your script
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', '1');
these settings will give you more info about your errors..
but most probably is that your config data:
DEFINE ('DB_USER', 'UserName');
DEFINE ('DB_PASSWORD', 'Password');
DEFINE ('DB_HOST', 'Localhost');
DEFINE ('DB_NAME', 'Databases');
is wrong.
check that effectively USER can use tables on DB_NAME w/ DB_PASSWORD
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
What NomikOS said but also, mysql_connect doesn't take a database name as an argument (mysqli_connect does, but that's another story). You want to do:
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
if (!$dbc) {
trigger_error ('Could not connect to MySQL: ' . mysql_error() );
}
$rc = mysql_select_db(DB_NAME, $dbc);
if (!$rc) {
trigger_error ('Could not select MySQL DB: ' . mysql_error() );
}
Hope this helps.