Jump to content
Larry Ullman's Book Forums

Chapter 8: Problem With Connecting To Mysql


Recommended Posts

I am on chapter 8 and I am at the point where I'm making the register.php file. (script 8.3) I coded the file myself the first time. All of the errors worked, but when you entered all the information correctly and hit submit, it redirected you to a page with only the header. there is nothing else on it. Then I used the script from the downloaded scripts and had the same problem. I looked at the 'sitename' database and nothing new was entered in the 'users' table. I'm not sure what to do. Here are the codes for the register.php file and the mysqli_connect.php file:

register.php:

<?php # Script 8.3 - register.php

$page_title = 'Register';
include ('includes/header.html');

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

$errors = array(); // Initialize an error array.

// Check for a first name:
if (empty($_POST['first_name'])) {
	$errors[] = 'You forgot to enter your first name.';
} else {
	$fn = trim($_POST['first_name']);
}

// Check for a last name:
if (empty($_POST['last_name'])) {
	$errors[] = 'You forgot to enter your last name.';
} else {
	$ln = trim($_POST['last_name']);
}

// Check for an email address:
if (empty($_POST['email'])) {
	$errors[] = 'You forgot to enter your email address.';
} else {
	$e = trim($_POST['email']);
}

// Check for a password and match against the confirmed password:
if (!empty($_POST['pass1'])) {
	if ($_POST['pass1'] != $_POST['pass2']) {
		$errors[] = 'Your password did not match the confirmed password.';
	} else {
		$p = trim($_POST['pass1']);
	}
} else {
	$errors[] = 'You forgot to enter your password.';
}

if (empty($errors)) { // If everything's OK.

	// Register the user in the database...

	require_once ('includes/mysqli_connect.php'); // Connect to the db.

	// Make the query:
	$q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";		
	$r = @mysqli_query ($dbc, $q); // Run the query.
	if ($r) { // If it ran OK.

		// Print a message:
		echo '<h1>Thank you!</h1>
	<p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p><br /></p>';	

	} else { // If it did not run OK.

		// Public message:
		echo '<h1>System Error</h1>
		<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; 

		// Debugging message:
		echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

	} // End of if ($r) IF.

	mysqli_close($dbc); // Close the database connection.

	// Include the footer and quit the script:
	include ('includes/footer.html'); 
	exit();

} else { // Report the errors.

	echo '<h1>Error!</h1>
	<p class="error">The following error(s) occurred:<br />';
	foreach ($errors as $msg) { // Print each error.
		echo " - $msg<br />\n";
	}
	echo '</p><p>Please try again.</p><p><br /></p>';

} // End of if (empty($errors)) IF.

} // End of the main Submit conditional.
?>
<h1>Register</h1>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"  /> </p>
<p>Password: <input type="password" name="pass1" size="10" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('includes/footer.html');
?>

 

mysqli_connect.php:

<?php # Script 8.2 - 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', 'root');
DEFINE ('DB_PASSWORD', 'censored');
DEFINE ('DB_HOST', 'MySQL');
DEFINE ('DB_NAME', 'sitename');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

?> 

 

I also tried the DB_HOST with localhost and nothing worked. I need help as soon as possible. You can see the page at Here

Link to comment
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

I would imagine that

DEFINE ('DB_HOST', 'MySQL');

 

should really be

 

DEFINE ('DB_HOST', 'localhost');

 

also remove the error supressor @ symbol before mysqli_connect,it may help you determine what the error is.

 

I did that, and what comes up now is

Fatal error: Call to undefined function mysqli_connect() in C:\Inetpub\vhosts\tagslash.com\httpdocs\includes\mysqli_connect.php on line 14

I am not sure what that means.Could you help?

Link to comment
Share on other sites

Is this a server on your PC or your live server? Check with your host or the php.ini file on your local server to see if you have mysqli function,s you possibly may only have the mysql_connect available to you.

 

phpinfo(); will tell you

I'm not sure what you mean. I am on a Windows 7 laptop. I downloaded XAMPP on my laptop, also I do not have a php.ini file on my computer. I did a search and nothing came up. What is my host, is it my hosting server that hosts my website, or is it phpMyAdmin? I appreciate the help.

Link to comment
Share on other sites

MySQL Support enabled

Active Persistent Links 0

Active Links 0

Client API version 5.0.51a

 

Directive Local Value Master Value

mysql.allow_persistent On On

mysql.connect_timeout 60 60

mysql.default_host no value no value

mysql.default_password no value no value

mysql.default_port no value no value

mysql.default_socket no value no value

mysql.default_user no value no value

mysql.max_links Unlimited Unlimited

mysql.max_persistent Unlimited Unlimited

mysql.trace_mode Off Off

 

this is the information under MySQL

Link to comment
Share on other sites

Try this at the very top of a new page

 

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

 

see if it connects now.

 

I don't think you have mysqli enabled, you hsould be able to do this: look in your xampp/php/ folder for a file called php.ini then look for mysql or mysqli and see if you can find that line and then remove the ';' to enable the mysqli functions

 

it should be this line i think

 

extension=php_mysqli.dll

 

make sure the ; that is probably there before the start of this line is removed. Then restart apache and run phpinfo() try and find mysqli again

Link to comment
Share on other sites

Try this at the very top of a new page

 

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

 

see if it connects now.

 

I don't think you have mysqli enabled, you hsould be able to do this: look in your xampp/php/ folder for a file called php.ini then look for mysql or mysqli and see if you can find that line and then remove the ';' to enable the mysqli functions

 

it should be this line i think

 

extension=php_mysqli.dll

 

make sure the ; that is probably there before the start of this line is removed. Then restart apache and run phpinfo() try and find mysqli again

 

I used the code and this is what comes up:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\Inetpub\vhosts\tagslash.com\httpdocs\includes\mysqli_connect1.php on line 2

Could not connect: Access denied for user 'root'@'localhost' (using password: YES)

 

and also, I found the php.ini file and the semicolon is already removed, but there are many more things that have semicolons. Here is the list of extensions:

extension=php_bz2.dll

;extension=php_curl.dll

;extension=php_dba.dll

extension=php_mbstring.dll

extension=php_exif.dll

;extension=php_fileinfo.dll

extension=php_gd2.dll

extension=php_gettext.dll

;extension=php_gmp.dll

extension=php_imap.dll

;extension=php_interbase.dll

;extension=php_ldap.dll

;extension=php_mssql.dll

;extension=php_mysql_mysqlnd.dll

extension=php_mysql.dll

;extension=php_mysqli_mysqlnd.dll

extension=php_mysqli.dll

;extension=php_oci8.dll

;extension=php_pdo_firebird.dll

;extension=php_pdo_mssql.dll

;extension=php_pdo_mysql_mysqlnd.dll

extension=php_pdo_mysql.dll

extension=php_pdo_odbc.dll

;extension=php_pdo_pgsql.dll

extension=php_pdo_sqlite.dll

;extension=php_pdo_sqlite_external.dll

;extension=php_pgsql.dll

;extension=php_pspell.dll

;extension=php_shmop.dll

;extension=php_snmp.dll

extension=php_soap.dll

extension=php_sockets.dll

extension=php_sqlite.dll

extension=php_sqlite3.dll

;extension=php_sybase_ct.dll

;extension=php_tidy.dll

extension=php_xmlrpc.dll

Link to comment
Share on other sites

Your phpinfo() output indicates the active PHP configuration file. That's the one that needs to be edited.

 

As for your access denied message, that means your root-user password is incorrect.

 

I searched it online and I learned that it meant that my root-user and password were incorrect, but I checked them and they were both right. If I'm using the root user, is the username "root" or "root-user"?

Link to comment
Share on other sites

What do you mean by you "checked them and they were both right"? The MySQL root user is just "root".

 

I checked the username and password, and both were correct. I also found that on my phpinfo() page, it says the Configuration File (php.ini) Path is C:\Windows when it is not located there. So I think that php knows where the php.ini file is.

Link to comment
Share on other sites

I checked the username and password, and both were correct.

 

Okay, so that doesn't actually tell anybody anything. HOW did you check? HOW did you know that they were correct? Because if they are correct, then they should work through a PHP script.

 

 

I also found that on my phpinfo() page, it says the Configuration File (php.ini) Path is C:\Windows when it is not located there. So I think that php knows where the php.ini file is.

 

Is that the *active* configuration file? And if it is, and there is no C:\\Windows\\php.ini, that means there is no active configuration file and you need to put one there.

Link to comment
Share on other sites

Okay, so that doesn't actually tell anybody anything. HOW did you check? HOW did you know that they were correct? Because if they are correct, then they should work through a PHP script.

 

 

 

 

Is that the *active* configuration file? And if it is, and there is no C:\\Windows\\php.ini, that means there is no active configuration file and you need to put one there.

 

I use the same password for everything, and I checked the letters and they were all correct, and "root" was spelled correctly. I also used the password to access MySQL and it worked. How do I know what the active configuration file is?

Link to comment
Share on other sites

Are you sure that it's where you say it is. Please check where it is configured. See the image.

 

da1jm.png

 

My php.ini file is locates in C:\xampp\php\php.ini on the phpinfo page it says it is in C:\Windows. Also on the Loaded Configuration File section, it says that it is in C:\Parallels\Plesk\Additional\PleskPHP5\php.ini but I do not have a Parallels folder on my computer.

Link to comment
Share on other sites

I don't really know what to tell you, I can't see how it's saying that it's pulling in a php.ini file from a location that doesn't exist, at this stage I would think that it must be there, but you don't know where it is. What windows are you running. Have you tried taking the location C:\Parallels\Plesk\Additional\PleskPHP5\php.ini and putting it into the run command bar? If you run windows 7 you can just type it it in letter by letter and it will work through the folders to get to it.

Link to comment
Share on other sites

I don't really know what to tell you, I can't see how it's saying that it's pulling in a php.ini file from a location that doesn't exist, at this stage I would think that it must be there, but you don't know where it is. What windows are you running. Have you tried taking the location C:\Parallels\Plesk\Additional\PleskPHP5\php.ini and putting it into the run command bar? If you run windows 7 you can just type it it in letter by letter and it will work through the folders to get to it.

 

Yes I tried to search it, and nothing came up. I have Windows 7.

 

I downloaded PHP earlier before I downloaded XAMPP and then I deleted PHP when I got XAMPP. Does that have anything to do with it? but even then, I didn't have those folders. The PHP folder was on my desktop.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share


×
×
  • Create New...