Nov 20, 2009

Problem With Mysql - Cannot connect to database....

free web hosting
Open Discussion & Free Web Hosting > Astahost > Hosted Members Support

Problem With Mysql - Cannot connect to database....

abhiram
Hi, this is the first time after I got my hosting to try and use MySQL. My site is http://abhiram.astahost.com. 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:

CODE

<?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:

CODE

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


Any Ideas?

Also, another question... does Astahost 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.

 

 

 


Comment/Reply (w/o sign-up)

mastercomputers
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.

CODE

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


Should be:

CODE

$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:

CODE

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

 

 

 


Comment/Reply (w/o sign-up)

abhiram
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.

Comment/Reply (w/o sign-up)

mastercomputers
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

Comment/Reply (w/o sign-up)

abhiram
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 smile.gif.

Comment/Reply (w/o sign-up)

abhiram
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:

CODE

<?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 biggrin.gif.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Mysql Database

  1. Mysql Connect Error, Too Many Connections. - (1)
    Just to begin with I want to say that I am not here necessarily to complain about it lol. I'm
    not sure but if my site is possibly helping to contribute to the problem, I'd like to know if
    anyone knows any good articles or tutorials that might have information on what to look out for that
    could contribute to these sort of problems. I know my site may not have anything to do with it (as
    I don't think it is overly popular or anything or even close), but knowing what to look out for
    when messing with PHP/mySQL would be great. I personally haven't found too...
  2. Connecting To Astahost's Mysql - (8)
  3. Mysql Server Not Stable After Hard Disk Swap On April 6th - (2)
    Hi... All are aware of the server issues happened on April 6th:
    http://www.astahost.com/current-server-iss...ade-t15435.html I'm still facing the issue with
    the MySQL Server after the server problems!! I'm using phpBB forum for my site, and it often
    shows some error message after losing connection with the Database server! Its happening many times
    in a minute! Its not at all stable after the server issues! I had tried repairing the databases and
    optimizing the databases! But nothing helped me! Still facing the issues! Its showing like:
    DEBUG MODE ...
  4. Site Not Opeing ( Phpbb Forum) :: Can't Connect To Local Mysql - (3)
    Hi all, Please help me! My site was working fine! ( www.fun.niranvv.com ) But now, its showing
    one error message as: Warning: mysql_connect() : Can't connect to local MySQL server
    through socket '/var/lib/mysql/mysql.sock' (2) in
    /home/niranvv/public_html/fun/db/mysql4.php on line 48 Warning: mysql_error(): supplied argument is
    not a valid MySQL-Link resource in /home/niranvv/public_html/fun/db/mysql4.php on line 330 Warning:
    mysql_errno(): supplied argument is not a valid MySQL-Link resource in
    /home/niranvv/public_html/fun/db/mysql4.php on line...
  5. What Is The MySQL Version @ Asta ? - MySQL 4.0.26-Max
    (16)
    Hi, A few question here, 1. Just wondering, the MySQL hosted on astahost.com is actually version
    "4.0.26-Max" according to phpmyadmin. But why is cPanel showing "4.1.21-standard"? 2. Actually
    i've existing codes that need function introduced after version 4.1.1, is there by any chance
    astahost or xisto is going to upgrade the MySQL package? Thanks...
  6. Problem With MySQL - (5)
    I tried to visit my website today and I recieved the dreadful page from my php-nuke site "There
    seems to be a problem with the MySQL database" First I thought, "no big deal server is probably
    rebooting or down for a little bit" So i logged into my cpanel and clicked on "Server Status".
    Thats when I started worrying. It said the MySQL servers were up and runnign fine. I went back to my
    main page and looked at my MySQL server count and instead of saying 2/99 it now says NA/99 I am
    unable to create a new database and I recieve this error when I click on PHPmyadmin QUO...
  7. Php And Mysql Version. - (7)
    Is there any plan to upgrade or see if it can be upgraded to PHP5 and mySQL5 in the future?...
  8. Mysql Error Never Seen On My Site Before (too Many Connections). - (13)
    I just tried to visit my site to see if anything had changed on my forums and such and I got this
    message: Warning: mysql_connect(): Too many connections in /xxxx/xxxxx/xxxxxx_xxxxx/xxx/xx_xxxx.php
    on line 7 Error! Could not connect: Too many connections. I have never recieved this error before,
    does this mean someone is mucking with my site somehow??? The particular php file it mentions is
    the one that defines variables like db username etc and connection to the site's database that I
    include wherever it is needed in various file for the site. EDIT: I managed t...
  9. Help Me! - Frontpage & phpBB with MySQL (3)
    I was just approved an Astahost account with the starter package. I explored the Cpanel a bit
    and have got the following problems : 1.Frontpage Extensions The Cpanel states that the
    Frontpage Extensions have been installed. But when I uploaded my site on frontpage at
    "ftp://ftp.mysitename.astahost.com/public_html/" it stated an error that Fronpage Extensions are not
    installed on this server! When I opened my site online, I can see the page, but I cannot see any
    graphics/images! Also, my forms and other stuff isn't working either. Please help me out and let
    me...
  10. Weird MySQL Message - (4)
    wenever i assign priviledges to a MySQL databases, it seems to work properly but this message is
    always there QUOTE bitshift_wforumu (Privileges: ALL PRIVILEGES) Connection Strings
    Perl $dbh = DBI->connect("DBI:mysql:bitshift_wforum:localhost","bitshift_wforumu"," ");
    PHP $dbh=mysql_connect ("localhost", "bitshift_wforumu", " ") or die ('I cannot connect to the
    database because: ' . mysql_error()); mysql_select_db ("bitshift_wforum"); My database
    name is: bitshift_wforum the username is: bitshift_wforumu What does all that maen, it ...
  11. Database Tables Gone? - (1)
    I wrote a simple web program/site in php that uses mysql as its database. The program stores a
    daily record of calories/serving. It's basically a calorie chart. I looked today and 2 of my
    tables were missing. Now considering it only uses 2 tables (which isn't too efficient but
    it's for personal use) that's a lot missing. It's not a big deal to lose the data and I
    didn't have a backup, but I'm just curious as to why and how it happened. Everything
    worked fine for awhile, then I decided to dabble with cronjobs. I wrote a cron to make ...
  12. Problem With MySQL Not Being Able To Connect - Anyone know the reason for this error... (9)
    Hello everyone, Im currently having a problem with mySQL, I wrote a PHP login script that
    doesnt seem to be working for some reason. I created a database and everything looks correct, is
    this a server issue or is the issue related to my script? But anyway this is my error, hope someone
    can help Ive tried to search forum for answer but none of the posts seems to answer my question:
    QUOTE Warning: mysql_connect(): Host 'gamma.xisto.com' is not allowed to connect to this
    MySQL server in /home/hp2001/public_html/ucscKGD/checklogin.php on line 10 cannot co...
  13. MySQL - Can't Connect - Cristal Report (1)
    From http://thoaionline.com (hosted by Astahost) QUOTE Warning: mysql_connect(): Can't
    connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in
    /home/pheart/public_html/thoaionline/includes/database.mysql.inc on line 31 Can't connect to
    local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) In cPanel Mysql
    databases 0 / 99 But I have 5 MySQL databases in my account (maybe more). All of them become
    unusable now. Some more messages from http://website.thoaionline.com QUOTE Warning: 
    mysql_pc...
  14. Can I Access MySQL Database From Outsite, Directly - (10)
    I'm going to make an OFFLINE FORUM. So I want to know is that possible or not. My paid host
    don't allow that. How about astahost?...
  15. Astahost Instable.. - now my database is gone (7)
    Hi there! I was just trying to access PHPmyadmin in the cpanel, but it told me to use the right
    username. Took me 2 minutes to figure out: No users, no databases set up. My site can't access
    it either. I had full databases installed and used them 2 hours ago! Ok, this is the third time in
    one week, that I have severe inaccessibility problems with astahost.. CUT I just tried again and now
    it is working the same way it was before. What the ®Çª?? Is this regular behaviour at astahost and
    do I just have to live with the fact that my site goes west twice a week without a...
  16. MySQL Database - Important Report (3)
    I'm writing this to report about a error. One of my mySQL database has been droped and my site
    stop working. I don't know the reason. I'm hosted on gamma server. I think you (admin)
    should check it. PS: One of my paid hosting provider also met this problem a few day ago....
  17. IPS Driver Error / MySQL Database - What in the world? How is this possible? (1)
    Just several hours ago, I couldn't access my website/forum because of some error with an SQL
    socket or something like that. I need desperate help because I don't want to reformat everything
    again. Well, here is what comes up: IPB WARNING mysql_connect(): Can't connect to local
    MySQL server through socket '/var/lib/mysql/mysql.sock' (2) (Line: 114 of
    /ips_kernel/class_db_mysql.php) There appears to be an error with the database. You can
    try to refresh the page by clicking here. Error Returned: SQL error: Can't connect to local
    M...
  18. Database Information? - (3)
    I'm trying to install 4images gallery but I'm having a problem with the database
    information. When I run the installer it asks me for these information: Database server hostname
    Database name Database username Database password What do I put for each one? Thanks....
  19. General Problems With New Account - MySQL problems and quota issues (2)
    I just got a new account and CPanel reports that I have -380.2 MB of quota space and using 400.2MB.
    This disables some of Cpanel's functions. It would have been nice to use the automated Mambo
    installer, but no biggy. I uploaded the latest Mambo .tar.gz from my FTP program and unpacked it
    via Cpanel's file manager with no problem. When I first logged into the system I created a new
    Database for mambo and a new user for that database and linked the two from Cpanel. I logged off to
    watch something on TV and came back and that database isn't being reported...
  20. Mysql Error - (5)
    Hey i got this error trying to install something on my database CODE mysql error: You have an
    error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the
    right syntax to use near 'rpg_class` (`name`, `folder`, `regatt`, `magicatt`, `magicdef` mysql
    error number: 1064 can anyone tell me what this means or how i fix it?? thanks in advance...
  21. MySQL Errors? - Error 13! - (0)
    Recently been attacked by "Can't create/write to file '/tmp/#sql_28e2_0.MYI' (Errcode:
    13)"? Then its a simple fix, please DELETE (backup first) your SOURCE files that talk to the MySQL
    database, and re-upload them This will purge them from the system and force them to recreate the
    temporary files. Do NOT delete your database, just backup your files, delete them, re-upload them
    and youll be fine. This is due to MySQL crash. Easily fixed and no data has been lost. Sorry guys
    Details: MySQL crashed while writing data, it seems the temporary file has bee...



Looking for problem, mysql, connect, database

See Also,

*SIMILAR VIDEOS*
Searching Video's for problem, mysql, connect, database
advertisement



Problem With Mysql - Cannot connect to database....

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com