Jump to content
Larry Ullman's Book Forums

Chapter 8: Problem With Connecting To Mysql


Recommended Posts

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

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.

 

Try copying the php.ini file from C:\xampp\php to C:\Windows, then stop and restart Apache.

Link to comment
Share on other sites

Try copying the php.ini file from C:\xampp\php to C:\Windows, then stop and restart Apache.

 

I'm still getting problems.

From the book's script, I got

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

 

AND

 

From the code given earlier in the forum, I got

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)

(but my password and username are correct)

Link to comment
Share on other sites

Do you have phpMyAdmin? Does it run? If so, when it loads select the mysql database, then browse the users table. Use the Insert link at the top of the page and add a new user/password, and be sure to give it some rights. Then try to connect using the new user in your connection script. Getting any closer?

Link to comment
Share on other sites

Do you have phpMyAdmin? Does it run? If so, when it loads select the mysql database, then browse the users table. Use the Insert link at the top of the page and add a new user/password, and be sure to give it some rights. Then try to connect using the new user in your connection script. Getting any closer?

 

I created a new user, but I'm still getting the same errors. The new user has all the privileges the root user has, and can do anything the root user can do. Not sure what else to do.

Link to comment
Share on other sites

Well, if your mysqli_connect.php file is stored inside the server root, inside the includes directory

http://localhost/name_of_site/includes/mysqli_connect.php

 

Make sure you have a working connection before you try to do more complicated coding and registration.

Link to comment
Share on other sites

1. What version of PHP are you using? If you're using PHP 4 you can't use the mysqli functions. But since you're on a Windows 7 laptop, I don't think you'd be using PHP 4.

 

2. Can you sign into phpMyAdmin with the username and password you say you checked and are correct?

 

3. Run the mysqli_connect.php script by itself and see if you can connect. If it's stored in the includes folder, just type the URL out, ending with the mysqli_connect.php script. For example...

 

http://localhost/sitename/includes/mysqli_connect.php

Link to comment
Share on other sites

1. What version of PHP are you using? If you're using PHP 4 you can't use the mysqli functions. But since you're on a Windows 7 laptop, I don't think you'd be using PHP 4.

 

2. Can you sign into phpMyAdmin with the username and password you say you checked and are correct?

 

3. Run the mysqli_connect.php script by itself and see if you can connect. If it's stored in the includes folder, just type the URL out, ending with the mysqli_connect.php script. For example...

 

http://localhost/sitename/includes/mysqli_connect.php

 

Yes, I've tried all of that and it still isn't working. The same errors come up on the mysqli_connect.php script itself, and I am running PHP 5.

Link to comment
Share on other sites

Also, you guys can look at everything on these links:

 

www.tagslash.com/register.php

 

www.tagslash.com/includes/mysqli_connect.php

 

www.tagslash.com/includes/mysqli1_connect.php (this page uses the script from earlier in the forum)

 

www.tagslash.com/phpinfo.php

 

The register.php script is using the mysqli_connect.php script.

 

Here are the scripts:

 

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', 'michaelli321');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
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() );

?>

 

mysqli_connect1.php:

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

Link to comment
Share on other sites

If you're testing your scripts on a real host, your DB_HOST is not localhost. Maybe it is, I don't know, I don't test my sites on real hosts right now. Only on my own development box.

 

Also, are you sure your database name is sitename?

Link to comment
Share on other sites

If you're testing your scripts on a real host, your DB_HOST is not localhost. Maybe it is, I don't know, I don't test my sites on real hosts right now. Only on my own development box.

 

Also, are you sure your database name is sitename?

 

Yes, my database name is sitename and the only table in it is users. What would my database host be then? I created my michaelli321 user using 'michaelli321'@'localhost'. Would my database host be the hosting service for my website? My MySQL doesn't have any connections to my website, only my PHP scripts.

Link to comment
Share on other sites

Mysqli is not enabled on your remote host. If you search the phpinfo.php page, you won't see it. You'll only see mysql. Get your web host to enable mysqli or use the older mysql functions, detailed in Larry's PHP for the Web book.

Link to comment
Share on other sites

Mysqli is not enabled on your remote host. If you search the phpinfo.php page, you won't see it. You'll only see mysql. Get your web host to enable mysqli or use the older mysql functions, detailed in Larry's PHP for the Web book.

What are the older mysql functions?

Link to comment
Share on other sites

  • 1 month later...

I am having the same problem and am getting pretty frustrated not being able to find a definitive answer to correct this. I assume that it is something simple, but I am at a loss.

 

mysqli_test.php is coded as follows:

 

<?php

$dbServer='localhost';

$dbUser='root';

$dbPass='pword';

$dbName='test';

$link = mysqli_connect($dbServer, $dbUser, $dbPass, $dbName) or die("Could not connect");

print "Connected successfully<br>";

mysql_close($link);

?>

 

The error:

 

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2003): Can't connect to MySQL server on 'localhost' (10061) in C:\xampp\htdocs\mysqli_test.php on line 6

I am running Apache 2.2.9, PHP 5.2.6, and MySQL 5

MySqli and MySql are both enabled in php.ini.

I am able to successfully log into MySQL via MySqlAdmin command line, MySql Command line, and MySql Workbench as root and all the other users that I have created.

I am able to manually connect to all databases and tables using root and all the other users I have created with the correct permissions.

 

The Default Port for both MySql and MySqli are set to 3307 (confirmed via phpinfo())

 

In my PHP.ini file:

log_errors =On

display_errors = On

From my PHP Error Log File:

[04-Oct-2011 12:40:44]

PHP Warning: mysqli_connect()

[<a href='function.mysqli-connect'>function.mysqli-connect</a>]:

(HY000/2003): Can't connect to MySQL server on 'localhost' (10061)

in C:\xampp\htdocs\mysqli_test.php on line 11

 

Anything I am missing?

Link to comment
Share on other sites

 Share


×
×
  • Create New...