Click here to Skip to main content
15,035,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code. It cannot connect to database.
PHP
class connect{
        // Check MySQL
        function chkmys(){
            if (mysql_errno()>0) die("MySQL error ".mysql_errno()." : ".mysql_error());
        }
        // Open a connection
        function open(){
            $hst = "127.0.0.1";
            $usr = "root";
            $pwd = ""; //KsfHMyDb#
            $dbs = "mine_db";
            $vsr = "D:\wamp\bin\mysql\mysql5.0.51b\data";
            if(file_exists($vsr)) {
                $cnx = mysql_connect($hst,$usr,$pwd);
                /* if (!$cnx) die('Failed to open connection to MySQL server');*/
                mysql_select_db($dbs,$cnx);
                connect::chkmys();
                return $cnx;
            }
        }
        // Close a connection
        function close($cnx){
            mysql_close($cnx);
            }
        }
Posted
Comments
phanny 2011 6-Dec-11 23:10pm
   
The output of this code is No database selected.

The code is more or less correct. You do not need to check if the path exists ( file_exists ).
Just call mysql_connect and it will fail if the MySQL is not available.

Please see examples on http://php.net/manual/en/function.mysql-connect.php[^]
   
User root does not necessarily have rights on all DBs.
If you have access to the DB please try this:

-Login in as root
- Do this query:

show grants for root@localhost;


It should give you this:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '<some_encrypted_pw>' WITH GRANT OPTION</some_encrypted_pw>


The important part is
PRIVILEGES ON *.*
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900