简体   繁体   中英

can't connect to mysql database using host,user and pass

I keep getting this whenever I run the below code:

Fatal error: Call to undefined function mysql_connect() in C:\\wamp\\www\\php-academy\\113-connect-db.php


    <?php
    $conn_error ='could not connect';
    $mysql_host = 'localhost';
    $mysql_user = 'root';
    $mysql_pass = '';
    mysql_connect($mysql_host, $mysql_user, $mysql_pass) or                                                   die($conn_error);

    $mysql_db = 'a_database';

    mysql_select_db($mysql_db) or die($conn_error);
    echo 'Connected!';
    ?>

That probably means you don't have the MySQL extension loaded. You can test whether the extension is loaded with extension_loaded("mysql"); .

I suspect that this is because the extension is not loaded. I would check your php_info(); to find out.

Once you do your code should work, although I prefer to assign the mysql_connect() to a variable and then call that variable in mysql_select_db() as I have below:

$defHost     = 'localhost';
$defUsername = 'username';
$defPassword = 'password';
$defDatabase = 'database_name';

$connect = mysql_connect($defHost, $defUsername, $defPassword) or die();
mysql_select_db($defDatabase, $connect) or die();

The reasons for the error may be the following:

Incorrect access details to server database are specified in configuration file. There are no read permissions for configuration file. Provide read permissions for the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM