-1

A quick newbie question: The following error messages turn up when running my code:

Notice: Use of undefined constant localhost - assumed 'localhost' in C:\xampp\htdocs\site\Test.php on line 3

Notice: Use of undefined constant root - assumed 'root' in C:\xampp\htdocs\site\Test.php on line 3 could not select database

The code is as follows:

<?php 
$c = mysql_connect('localhost', 'root', '');
mysql_select_db('my_database', $c) or die ("could not select database!!");
?>

I've done a little searching on the web, and for the most part when something similar turns up it seems to be a case of people forgetting to put there literals into quotes. I have in this case, so it makes it all the more confusing for me. Is it perhaps an issue with the php.ini file do you think? thanks for any help.

MeMory LEAk99
  • 295
  • 1
  • 6
  • 12
  • try 127.0.0.1 see if it works – KJYe.Name Mar 09 '11 at 22:03
  • 10
    It looks more like your code is actually `mysql_connect(localhost, root, '');` (without the quotes). – Felix Kling Mar 09 '11 at 22:05
  • 1
    Are you sure it is this script you are calling and throwing the error? The error message is clear, I expect you forgot your quotes. But the line number 3 does not match with the code you provided here. – Tim Mar 09 '11 at 22:06
  • 4
    You are sure, that you look into the right file? I ask, because the "line 3" is the line with `mysql_select_db()` and neither `localhost`, nor `root` are there. – KingCrunch Mar 09 '11 at 22:07
  • Wired. This is shut in the dark, but maybe you have two scripts one with `mysql_connect(localhost, ...)` and and call that another one or maybe you have two folders or maybe you don't refresh the browser, because it's looks right to me. – jcubic Mar 09 '11 at 22:12
  • See also http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean – John Carter Nov 06 '11 at 06:04

3 Answers3

0

I suspect you are failing to connect but you don't test the handle, so you are passing an invalid handle in the select_db (i.e. $c). If no link is found, behavior is to attempt a mysql_connect without parameters which defaults to 'root' and 'localhost', possibly misdirecting you as to the cause.

horatio
  • 1,426
  • 8
  • 7
0

Curses. This one rings a bell, and I simply can't remember what it was...

However, I'd recommend not hardcoding connection details anyway.

I'd declare hostname,user name, password and database name as variables, and pass them into the DB.

For larks, I bet if you append an "or die" on line 2, you'll see it explode there - $c is never really set.

Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
  • Out of curiosity, whats the advantage of passing in the values as arguments? – MeMory LEAk99 Mar 09 '11 at 23:33
  • They are items that are likely to change between environments - development, test, production etc. I'd recommend pushing them into separate configuration files, so you don't have to dive deep into your database access code to modify them when pushing to a different environment - especially if you open your database connections in more than one place. – Neville Kuyt Mar 10 '11 at 13:25
0

Ah, the problem has seemed to resolve itself strangely enough - it's either between me changing the case of the url I was using/ my temporary change in browser / the edits to my code not registering when I save.

Thanks for taking the time to try and help me out.

MeMory LEAk99
  • 295
  • 1
  • 6
  • 12