FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Problem connecting to MySQL database with PHP [RESOLVED]





rfwrangler
I am having trouble connecting to a MySQL database from a PHP script. The database and site are hosted with Frihost.

I have the server set to localhost

the code that I am using is:
$connection = mysql_pconnect("$dbhost","$dbusername","$dbpassword")
or die ("Couldn't connect to server!");
$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database!");

With $dbhost set to localhost
$dbusername set to the username of the database
$dbpassword as the password to the database

I am not sure if I have these set up right for this database.

Any help that anyone could provide would be greatly appreciated.
SpellcasterDX
Try posting the php file in here. It might be easier to help you if we see the file. Maybe it has something to do with the php file and not the SQL variables.
rfwrangler
here is the full code of the PHP file I am using an required file for opening the db and the db access code is in that file.

Code:
<?php

$dbhost = 'localhost';
$dbusername = 'my_db_username';
$dbpassword = 'my_db_password';
$database_name = 'rfwrangl_TMMain';

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpassword")
   or die ("Couldn't connect to server!");
$db = mysql_select_db("$database_name", $connection)
   or die("Couldn't select database!");
?>


This is the PHP file that the required is in.

Code:
<?php
error_reporting(E_ALL);
//Include the database connection files to connect to any databases where the registered users are
//to be held.  This could be multiple databases depending on the needs.
include 'Resources/PHPModules/db_TMMain.php';
//Get to POST variable from the registration form and set them to PHP variables that we can work with.
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['user_name'];
$pwdrecquest = $_POST['pwdrecquest'];
$pwdrecanswer = $_POST['pwdrecanswer'];
$user_info = $_POST['user_info'];
//Check and remove slashes from escaped characters. 
//These characters could cause problems with the script.
$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$pwdrecquest = stripslashes(pwdrecquest);
$pwdrecanswer = stripslashes(pwdrecanswer);
$user_info = stripslashes($user_info);
//Error checking to make sure that required fields have been submitted.
//If there is an error the join_now.html file will be shown again so that they can fix any errors.
//The script will tell them what errors there were.
if((!$first_name) || (!$last_name) || (!$email_address) || (!$username) || (!$pwdrecquest)
   || (!$pwdrecanswer)) {
      echo "You forgot to fill in the following required information!<br />";
      if(!$first_name) {
         echo "First Name is a REQUIRED FIELD.&nbsp;&nbsp;Please enter it below.<br />";
      }
      if(!$last_name) {
         echo "Last Name is a REQUIRE FIELD.&nbsp;&nbsp;Please enter it below.<br />";
      }
      if(!$email_address) {
         echo "Email Address is a REQUIRED FIELD.&nbsp;&nbsp;Please enter it below.<br />";
      }
      if(!$username) {
         echo "Desired Username is a REQUIRED FIELD.&nbsp;&nbsp;Please enter it below.<br />";
      }
      if(!$pwdrecquest) {
         echo "Password Recovery Question is a REQUIRED FIELD.&nbsp;&nbsp;Please enter it below.
            <br />";
      }
      if(!$pwdrecanswer) {
         echo "Password Recovery Answer is a REQUIRED FIELD.&nbsp;&nbsp;Please enter it below.<br />";
      }
      include 'Resources/HTML/join_now.html';//Show form again.
   exit();
}
//Check to see if user exists in member database.  If the user name or email address has been entered in
//the database then alert the user and display the form again.
$sql_email_check = mysql_query
   ("SELECT email_address FROM members WHERE email_address='$email_address'");
$sql_username_check = mysql_query
   ("SELECT username FROM members WHERE username='$username'");
$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);
if(($email_check > 0) || ($username_check > 0)) {
   echo "Please fix the following errors:<br />";
   if ($email_check > 0) {
      echo "The Email Address that you have enter is already entered in to our member registry.&nbsp;
      &nbsp;If you have already signed up at our site please recover your username and password from
      the <a href=\"/password_recovery.php\">Username and Password Recovery</a> page.&nbsp;&nbsp;If
      this is your first time registering please enter a different Email Address below.<br />";
      unset($email_address);
   }
   if ($username_check > 0) {
      echo "The Desired Username that you have enter is already entered in to our member registry.&nbsp;
      &nbsp;If you have already signed up at our site please recover your username and password from
      the <a href=\"/password_recovery.php\">Username and Password Recovery</a> page.&nbsp;&nbsp;If
      this is your first time registering please enter a different Desired Username below.<br />";
      unset($username);
   }
   include 'Resources/HTML/join_now.html';
   exit();
}
//If everything has check out so far then we can add the user to the member registry.
/*Random Password generator.  Thank you PHPFreak!
http://www.phpfreaks.com/quickcode/Random_Password_Generator/56.php*/
//We will generate a random password for the user and encrypt it.  We do this as a security measure when
//the user signs up at the site they will have to login with this password to activate their account.
//This area will enter the member information into the database and email the user with the information.
function makeRandomPassword() {
   $salt = "abcdefghijklmnopqrstuvwxyz01234567890";
   srand((double)microtime()*1000000);
      $i = 0;
      while ($i <= 7) {
         $num = rand() % 33;
         $tmp = substr($salt, $num, 1);
         $pass = $pass . $tmp;
         $i++;
      }
   return $pass;
}
$random_password = makeRandomPassword();
$db_password = md5($random_password);
//Enter infomation in the database.
//This is changed a little from the PHPFreak tutorial so that we enter the decrypted password into the
//database so that we can use it later in an email to the user and for password recovery if needed.
$user_info2 = htmlspecialchars($user_info);
$sql = mysql_query
   ("INSERT INTO members (first_name, Last_name, email_address, username, pwdrecquest, pwdrecanswer,
    user_info, decrypted_password, password, signup_date)
       VALUES
   ('$first_name','$last_name','$email_address','$username','$pwdrecquest','$pwdrecanswer','$user_info2',
   '$random_password','$db_password',now())") or die (mysql_error());
if (!$sql) {
   echo "There has been an error creating your account.&nbsp;&nbsp;Please contact the
      <a href=\"mailto:webmaster@rfwrangler.frih.net\">Webmaster</a>!";
   } else {
      $userid = mysql_insert_id();
//Email the user with the account information that was just established.
      $subject = "Thank you for registering with the Eureka Toastmasters Website";
      $message = "Dear $first_name $last_name,
      You are now registered at the Eureka Toastmasters website.
      
      To activate your membership, please login at http://rfwrangler.frih.net
      
      Once you activate your membership you will be able to login with the following information:
         Username: $username
         Password: $random_password
      Please keep this information where you have access to it.  Once you login you will be able to change
      your password to something that is easier to remember and finish filling out your profile.  Just go to
      the profile page after you have logged in to the website.
      
      Thank you for registering with the Eureka Toastmasters website.
      
      Thank you,
      Webmaster
      Eureka Toastmasters
      
      This is an automated response, please do not reply!";
      mail
   ($email_address, $subject, $message, "From: Eureka Toastmasters Website <noreply@rfwrangler.frih.net>
   nX-Mailer: PHP/" . phpversion());
      echo "Your membership information has been mailed to your email address.&nbsp;&nbsp;Please, check
         your email and follow the instructions.";
}             
?>


This is to process a user registration form and generate a random password then that inforamtion is put in the database.

I am working on the database connection files for my site right now and I need to make sure that I am getting this correct before I write a lot of code that doesn't work.

Thank you for any help with this.
rfwrangler
I had several TYPOS in the scripts adn I have them working now. Thank you for the assistance and the clue to look elsewhere.
Animal
Topic is [RESOLVED] so I'll -close- it. Please PM any moderator if you want this topic re-opened.
Related topics
Mysql And PHP HELP PLZ
Mysql database problem
Mysql database size?
[MySQL database] Password and username
News system with mySQL database.
insert data to mySQL database problem
Delete data from MySQL database problem!!
MySQL database
Connecting to 2 different Mysql database and tables?
PHP that creates subdomain, ftp account, and mysql database?
Changing entries in MySQL database
[help]Mysql & php problem
Connecting to a MySQL database using Python
Problem with a MySQL database
This topic is locked: you cannot edit posts or make replies.    Frihost Forum Index -> Support and Web Hosting -> Web Hosting Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.