[PHP/MySQL] Cannot connect to database.

Newbie Spellweaver
Joined
Mar 28, 2009
Messages
5
Reaction score
1
Like the title says, I keep getting that error while trying to create a user on my website.. Hoping you guys can help out. (Note that you may have to use very simple terms, as I am not a god of PHP/MySQL, I actually just started trying to learn PHP)

Im following this guide: http://www.trap17.com/index.php/php-simple-login-tutorial_t7887.html
My website I'm trying to register on: http://coregaming.byethost9.com/register.php?
And here's my dbConfig.php: (blocked out password)

Code:
<?
// Replace the variable values below
// with your specific database information.
$host = "localhost";
$user = "b9_3354766";
$pass = "********";
$db   = "b9_3354766_index";

// This part sets up the connection to the 
// database (so you don't need to reopen the connection
// again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}

// Then you need to make sure the database you want
// is selected.
mysql_select_db($db);
?>

I'm assuming it's because the db host is localhost and it's not hosted on my computer.. I really just don't know.
 
Yeah, that would be why for sure. Localhost is on your machine. Where's the database hosted? If it's on your website, put in your website name, that might work. Otherise you'll have to look for settings that your hoster gives you
 
Hahah. I looked around this hosts cpanel and it says what to use for the host thing. My bad, I should have looked harder. Problem resolved and stuff.

EDIT: Oooor not. It doesn't register the users, which I think is also due to something earlier. Here's my table..

dbUsers

Kumar94 - [PHP/MySQL] Cannot connect to database. - RaGEZONE Forums
 
Hahah, sorry, I was in a rush and forgot to put the codes. You've already got dbConfig.php, so here's register.php and login.php (doubt you need logout.php)

Register:

Code:
<?php

// dbConfig.php goes under this
include ("dbConfig.php");


//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
 {
 $bInputFlag = false;
 foreach ( $_POST as $field )
 	{
 	if ($field == "")
   {
   $bInputFlag = false;
   }
 	else
   {
   $bInputFlag = true;
   }
 	}
 // If we had problems with the input, exit with error
 if ($bInputFlag == false)
 	{
 	die( "Problem with your registration info. "
   ."Please go back and try again.");
 	}

 // Fields are clear, add user to database
 //  Setup query
 $q = "INSERT INTO `dbUsers` (`username`,`password`,`email`) "
 	."VALUES ('".$_POST["username"]."', "
 	."PASSWORD('".$_POST["password"]."'), "
 	."'".$_POST["email"]."')";
 //  Run query
 $r = mysql_query($q);
 
 // Make sure query inserted user successfully
 if ( !mysql_insert_id() )
 	{
 	die("Error: User not added to database.");
 	}
 else
 	{
 	// Redirect to thank you page.
 	Header("Location: register.php?op=thanks");
 	}
 } // end if


//The thank you page
elseif ( $_GET["op"] == "thanks" )
 {
 echo "<h2>Thanks for registering!</h2>";
 }
 
//The web form for input ability
else
 {
 echo "<form action=\"?op=reg\" method=\"POST\">\n";
 echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
 echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
 echo "Email Address: <input name=\"email\" MAXLENGTH=\"50\"><br />\n";
 echo "<input type=\"submit\">\n";
 echo "</form>\n";
 }
// EOF
?>

Login:

Code:
<?php
session_start();
// dBase file
include "dbConfig.php";

if ($_GET["op"] == "login")
 {
 if (!$_POST["username"] || !$_POST["password"])
 	{
 	die("You need to provide a username and password.");
 	}
 
 // Create query
 $q = "SELECT * FROM `dbUsers` "
 	."WHERE `username`='".$_POST["username"]."' "
 	."AND `password`=PASSWORD('".$_POST["password"]."') "
 	."LIMIT 1";
 // Run query
 $r = mysql_query($q);

 if ( $obj = @mysql_fetch_object($r) )
 	{
 	// Login good, create session variables
 	$_SESSION["valid_id"] = $obj->id;
 	$_SESSION["valid_user"] = $_POST["username"];
 	$_SESSION["valid_time"] = time();

 	// Redirect to member page
 	Header("Location: members.php");
 	}
 else
 	{
 	// Login not successful
 	die("Sorry, could not log you in. Wrong login information.");
 	}
 }
else
 {
//If all went right the Web form appears and users can log in
 echo "<form action=\"?op=login\" method=\"POST\">";
 echo "Username: <input name=\"username\" size=\"15\"><br />";
 echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
 echo "<input type=\"submit\" value=\"Login\">";
 echo "</form>";
 }
?>
 
Back