how to make it work? MySql -help needed...please

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

how to make it work? MySql -help needed...please

Post by crazytopu »

When i use the following two lines i can get connected to my MySql DB without any problem..

Code: Select all

$db = mysql_connect("localhost", "root"); 

mysql_select_db("library",$db);
However, when i try to use the following piece of code to get connected to my MySql DB it gives me an error.

Code: Select all

//db_connect.php

<?php
        $db_user = 'localhost
        $db_pass = 'root
        $db_host = 'localhost'; //Usually "localhost"
        $db_name = 'library';

	$con= @mysql_connect("$db_host","$db_user","$db_pass")
	or die ("Cannot connect to MySQL.");

	$db = @mysql_select_db("$db_name",$con)
	or die ("Cannot select the $db_name database. Please check your details in the database connection file and try again");
	
 ?>
This is the error messege i receive:

Code: Select all

Cannot connect to MySQL.
So, i did remove these lines of code from my db_connect.php file :

Code: Select all

or die ("Cannot connect to MySQL.");

or die ("Cannot select the $db_name database. Please check your details in the database connection file and try again");
I got this reply after i tried that:

Code: Select all

Parse error: parse error, unexpected T_VARIABLE in c:\program files\apache group\apache\htdocs\db_connect.php on line 10
So, it is quite clear my php code cant get connected to DB. Why is that being happening where i m using the same username and password?

This is how i always get my Mysql DB up and running...do you see any problem here ? or do you want to suggest a better way that is more widely used?

Code: Select all

i put my "mysql" in C:\

Then I go to C:\mysql\cd bin and  click on mysqladmin icon..it asks for a username and password..After i finished providing the necessary information it comes to stay on my status bar and shows green light when the Db server is on and red when it is off....Am i doing anything wrong by starting MySql server like that?


Can anybody sheds some light?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try this..

Code: Select all

<?php
  $db_user = 'the user';
  $db_pass = 'thepassword';
  $db_host = 'localhost'; //Usually "localhost"
  $db_name = 'library';

  $con = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
  mysql_select_db($db_name) or die(mysql_error());
?>
If it doesn't work it should at least tell you why ;)
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

i tried this [your code]:

Code: Select all

$db_user = 'localhost'; 
  $db_pass = 'root'; 
  $db_host = 'localhost'; //Usually "localhost" 
  $db_name = 'library'; 

  $con = mysql_connect($db_host, $db_user, $db_pass) 
		or die(mysql_error()); 
  

  mysql_select_db($db_name) 
	or die(mysql_error());
And got this error messege:

Code: Select all

Warning: mysql_connect(): Access denied for user: 'localhost@localhost' (Using password: YES) in c:\program files\apache group\apache\htdocs\db_connect.php on line 9
Access denied for user: 'localhost@localhost' (Using password: YES)
What to do now?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Then you need to enter/find out the correct details, i.e the mysql user and password. A user named 'localhost' sounds a bit strange so i'd check that's correct first, same goes for a password of 'root'.

Sure you shouldn't be connecting as user root with a password? (shouldn't be using root user but it sounds more likely than localhost ;))
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Thanks..............it is actually

username: root
password: //no password..

have a nice day.........
Post Reply