Jump to content
Larry Ullman's Book Forums

Script 8.2, Connect To Db And Define


Recommended Posts

Hi,

I think I've messed this one up. Here's what I did.

 

I typed up the above script exactly. I didn't have a password set-up for 'root' so I typed DEFINE('DB_PASSWORD', '""');

It didn't work (Could not connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES)).

I thought that maybe it doesn't like not having a password so I went into XAMPP security and set up a password for the 'root' access.

I tested this by setting up a DB connection in Dreamweaver and it tested and connected OK. So I know that my username, password and host names are correct.

I changed the DEFINE part of the php file as follows - DEFINE ('DB_PASSWORD', 'mypassword');.

It didn't work. Same error message.

I then thought, oops, it's because the DB_PASSWORD as been set up as the constant '""'.

 

Can a constant be changed or deleted? If not (which I suspect is the case, otherwise it would be a bit of a misnomer) where do I go from here? Out of interest is the constant value stored somewhere on the webserver?

 

Thanks once again.

Paul

Link to comment
Share on other sites

I am confused. From what i think your saying you know that your script works because it did in Dreamweaver?? But you've changed your constant and it still doesn't work?? Is that what your saying?

 

The constants can be changed as you have done, so you shouldn't have any problems. I would guess either your referencing the wrong file or you've not set the constants up.

 

What does the error say?

What code are you using?

  • Upvote 1
Link to comment
Share on other sites

Paul, it looks like for your original (no password) password, you have double quotation marks within single quotation marks. That won't work. If you truly don't have a password, just use two single quotation marks. Remember that single quotation marks interpret everything literally, so doing '""' (double quotation marks within single quotation marks) will make PHP think that the password is two double quotation marks, which I don't even think is a valid password.

 

Also, remember that DB_PASSWORD is the constant, not the value set for it. Semantics, perhaps, but a constant can be changed at any time by simply changing the value in the DEFINE statement. Also, it can be erased by erasing the whole statement. It is not set on the server anywhere but the script where you define it.

 

All I can say, without knowing more about your environment and what's in the script is that if your password is mypassword, then DEFINE ('DB_PASSWORD', 'mypassword'); "should" work.

 

Please let us know if we can help anymore. Thanks. I imagine that there's a conflict between your phpMyAdmin password settings and what's being used for your script.

  • Upvote 1
Link to comment
Share on other sites

Hi,

I actually got it to work eventually but I wanted to wait for a reply because I'm not clear on what DEFINE is?

 

To correct it I changed the statement from:

 

DEFINE ('DB_USER', 'root');

DEFINE ('DB_PASSWORD', 'mypassword');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'forum');

 

// Make the connection

 

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

 

To:

 

DEFINE ('DB_USER', 'root');

DEFINE ('DB_pass', 'mypassword');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'forum');

 

// Make the connection

 

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_pass, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

 

Which is, HartleySan, what you are saying. It's DB_PASSWORD that's the constant. I changed it to DB_PASS. But if the constant is only in the script then presumably DB_PASSWORD is gone forever. Which doesn't, in my mind, make it much of a constant.

 

I guess I'm a bit confused about the point of constants or the DEFINE function. I was under the impression that once a DEFINE was defined it couldn't be changed...fullstop. Could the same thing be done just by creating a variable? What's the difference?

 

Cheers.

Link to comment
Share on other sites

Constants are like variables in a sense that they store information. But once you've set a Constant it can't be changed. Unlike a variable that can be altered as your scripts progress. For instance:

<?php
$var = 5;
$var = $var * $var;
echo $var;
// Prints 25
?>

 

The constant provides a good way to store your database information and use it in a query. The values of your DB connection info are never going to change unlike like the potential changes a variable can have done to it through a script.

 

You are right about DB_PASSWORD being gone forever, but think of the alternative, every constant you defined stays there alive forever.. They are just constant in the 'life' of the scripts or until you remove them from your script.

 

You could just free type

$dbc = mysqli_connect('localhost', 'root', '', 'forum');

 

and you'd get the same results but its better to dynamically reproduce that line.

 

To be honest I can't see why your first attempt:

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'mypassword');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'forum');

 

errors. HartleySan found the error ('""') so you could just have left the constant as it was and altered its value as suggested. All you've done is change one constants name but it has the same value and is being used in the same place ias it was before. Did you get an error using the above code? You may need to remove the '@' from the query to get the error to occur I've not checked in a great detail if there is any errors in that set of rules, but on face value it looks ok.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...