Jump to content
xisto Community
Sign in to follow this  
abhiram

Problem With Mysql Cannot connect to database....

Recommended Posts

Hi, this is the first time after I got my hosting to try and use MySQL. My site is http://forums.xisto.com/no_longer_exists/. I created a user in CPanel -> 'MySQL Databases' as root. So, the new user created will be 'abhiram_root'. Similarly I created a database called 'webpage' (which changes to 'abhiram_webpage'). I set up the database and the tables using phpMyAdmin and uploaded the files after making changes to the user name and the database.

But it's not opening the database. It's connecting to MySQL without any problems, but it isn't opening the database. Here's the php code I've used:

<?php//tempuser config.php$dbhost='localhost';$dbuser='abhiram_root';$dbpass='*****';//opendb.php$conn=mysql_connect($dbhost,$dbuser,$dbpass) or die('Error with given username and password');$database='abhiram_webpage';mysql_select_db($database) or die("Error, could not open database");	$query="select title,content,time from blog order by id desc";	$title="select phrase from subheading order by rand()";	$result=mysql_query($query) or die('Error, could not retrieve data from table');	$result2=mysql_query($title) or die('Error, could not retrieve data from table');	$row2=mysql_fetch_array($result2);	list($result2)=$row2;?>

It keeps giving me the error message from this line:

mysql_select_db($database) or die("Error, could not open database");

Any Ideas?

Also, another question... does Xisto have global_variables enabled or not? Because I've got a PHP script to change the background which works only if global_variables are enabled.

Thanks.

Share this post


Link to post
Share on other sites

OK abhiram,

This may not be the problem but you've not actually connected the database, because you have not actually said in your code to do anything to it.

This line only sets $conn, it doesn't actually show that we connected although this can sometimes work, but it's best to make sure it does.

$conn = mysql_connect( $dbhost, $dbuser, $dbpass ) or die( 'Error with given username and password' );

Should be:

$conn = @mysql_connect( $dbhost, $dbuser, $dbpass );// this makes a definite connection check and reports an error if any// since $conn can still be set with an error message and not be considered// a failure.if( !$conn ) die ( 'Error with given username and password' );

As you can see here, I am checking if the connection actually works, because we shouldn't instantly assume that we connected by setting it, since an error string being sent can still be valid. At least now we can make sure we're connecting. The @ sign is just to suppress any error other than what we want to show. You could test this by echoing the results of $conn if you don't think this is the case.

To be on an even safer side we could do this as well which adds the connection link we wanted to use so to not let PHP guess which connection to choose even though it usually defaults to the last connection made:

mysql_select_db($database, $conn) or die('Error, could not open database.');

The only other problem would be, are you positive that's the correct database? Since that would be the most obvious at that stage without the above failing.

Hope this helps.

As for global variables, you mean autoglobal variables? You should be able to do away with autoglobal variables, this is not a set back, global variables exist but must be explicitly defined if you need them. So you should fix your code up if you rely on autoglobal or auto registered variables.

Cheers,


MC

Share this post


Link to post
Share on other sites

Hi MC,Thanks for the quick reply. Well... what I've used was working on my computer. I've tried changing the code to what you suggested, but am still getting the same error. The first thing I checked was the database name. It's the same ... atleast that's what is shown in phpMyAdmin. For the user I've created to connect to the database, I've given permissions as 'All'. Is there any super-user username and password I can use to connect to the database? Maybe, this database isn't being given access to that particular user.

Share this post


Link to post
Share on other sites

I would look into your cPanel and look at MySQL Databases under Site Management Tools and see if you can notice anything different from the databases, username, location etc. MySQL Database would be the way I would create users, databases etc and I would use phpMyAdmin just to administrate it, but I usually do everything from commandline than in a web GUI.Someone with All rights can be limited to a database but if this is your root, you shouldn't have limited it and made sure they had access to all databases.If possible, just create another username with ALL rights, another database and then just write another script to test this database out. It could be a setup problem, yet I can only guess that. You could also attempt to backup or if you have a backup of your database, remove everything from MySQL Database and start afresh. Remember to not prefix your user as this is automatically done, which I think you already know.Cheers,MC

Share this post


Link to post
Share on other sites

I've checked the permissions that I've given to the user I created. It's got 'All' the permissions. I still can't figure out why it isn't working.I guess I'll just have to create another database and write a test script to access it. I'll post back with the results I get. Thanks for your time B).

Share this post


Link to post
Share on other sites

Well... I got that fixed. I still can't figure out the reason. After clicking on 'Grant' all privileges to the user, it displayed some code to use to connect to the database. Just by replacing the code with that did the trick. Here's the final code. I've commented out the old code just to show the difference:

<?php//tempuser config.php$dbhost='localhost';$dbuser='abhiram_root';$dbpass='******';//opendb.php $dbh=mysql_connect ("localhost", "abhiram_root", "******") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("abhiram_webpage") or die("Error, could not open database");/* $conn=@mysql_connect($dbhost,$dbuser,$dbpass);if(!$conn) die('Error with given username and password');$database='abhiram_webpage';mysql_select_db($database) or die("Error, could not open database"); */ $query="SELECT title,content,time FROM blog ORDER BY id DESC"; $title="SELECT phrase FROM subheading ORDER BY RAND()"; $result=mysql_query ($query) or die('Error1, could not retrieve data from table'); $result2=mysql_query ($title) or die('Error2, could not retrieve data from table'); $row2=mysql_fetch_array($result2); list($result2)=$row2;?>

Now, I've got several other problems to address B).

Share this post


Link to post
Share on other sites

Sorry for the double post... but it seems like global variables is 'on'. Because my website is working fine.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.